You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.3 KiB

  1. import boto3
  2. import zipfile
  3. ################################################################################################
  4. #
  5. # Configuration Parameters
  6. #
  7. ################################################################################################
  8. # you need to create a bucket in S3, here in this demo it is called "cloudcomp-counter", but
  9. # bucket names need to be world wide unique ;) The demo looks for a file that is named
  10. # "us-east-1" (same as our default region) in the bucket and expects a number in it to increase
  11. # region = 'eu-central-1'
  12. region = 'us-east-1'
  13. functionName = 'cloudcomp-counter-lambda-demo'
  14. # roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
  15. roleName = 'arn:aws:iam::919927306708:role/cloudcomp-s3-access'
  16. ################################################################################################
  17. #
  18. # boto3 code
  19. #
  20. ################################################################################################
  21. client = boto3.setup_default_session(region_name=region)
  22. lClient = boto3.client('lambda')
  23. apiClient = boto3.client("apigatewayv2")
  24. print("Deleting old function...")
  25. print("------------------------------------")
  26. try:
  27. response = lClient.delete_function(
  28. FunctionName=functionName,
  29. )
  30. except lClient.exceptions.ResourceNotFoundException:
  31. print('Function not available. No need to delete it.')
  32. print("creating new function...")
  33. print("------------------------------------")
  34. zf = zipfile.ZipFile('lambda-deployment-archive.zip', 'w', zipfile.ZIP_DEFLATED)
  35. zf.write('lambda_function.py')
  36. zf.close()
  37. lambdaFunctionARN = ""
  38. with open('lambda-deployment-archive.zip', mode='rb') as file:
  39. zipfileContent = file.read()
  40. response = lClient.create_function(
  41. FunctionName=functionName,
  42. Runtime='python3.8',
  43. Role=roleName,
  44. Code={
  45. 'ZipFile': zipfileContent
  46. },
  47. Handler='lambda_function.lambda_handler',
  48. Publish=True,
  49. )
  50. lambdaFunctionARN = response['FunctionArn']
  51. print("creating API gateway...")
  52. print("------------------------------------")
  53. #apiArn = ""
  54. response = apiClient.create_api(
  55. Name=functionName + '-api',
  56. ProtocolType='HTTP',
  57. Target=lambdaFunctionARN
  58. )
  59. #apiArn=response['']
  60. #response = lClient.create_event_source_mapping(
  61. # EventSourceArn=apiArn,
  62. #)