aws lambda - How to add s3 trigger using cloudformation if the s3 bucket is created manually

Viewed 191

I have created my s3 bucket "myBucket" manually.

And I need a lambda function with s3 trigger.

    "Parameters": {
      "HandlerCodeS3Bucket": {
        "Type": "String",
        "Default": "myBucket"
      },
    },
    "Resources": {
      "uploadBannerToS3Handler": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
          "FunctionName": "uploadBannerToS3",
          "Handler": "uploadBannerToS3.handler",
          "Role": {
            "Fn::ImportValue": {
              "Fn::Sub": "${RolesStack}-LambdaRoleArn"
            }
          },
          "Code": {
            "S3Bucket": {
              "Ref": "HandlerCodeS3Bucket"
            },
            "S3Key":"uploadBannerToS3.zip"
          },
          "Layers": [
            {
              "Ref": "MyLayer"
            }
          ],
          "Runtime": "nodejs12.x"
        }
      }
          ...
    }

How should I modify the code to add s3 trigger in my function.

Also, for development, is it more convenient if I build my s3 bucket in cloudformation also, if I will build my UAT environment in the future?
How would developer normally do for s3 if using cloudformation?

1 Answers

If you created your bucket manually, then you can't use it in CloudFormation (CFN) the way you are trying to do. You either have to leave bucket creation and management to CFN, or if you really want, you can import existing bucket into CFN.

I still need to manually upload my lambda zip files to s3 manually,

Yes. CFN will not upload it for you as it has no access to your local workstation. But AWS CLI has commands such as package that can make the upload process easier.

Related