2
votes

Thanks in advance!

So I currently have a cloud formation script that creates a lambda function, the code in the lambda function needs to reference an s3 bucket that also needs to be generated by the same cloudformation script. The code for the lambda function is stored in a pre-cloudformation bucket which is pulled in when it is ran.

The problem I have is how would I cater for a programatically generated bucket name in my node.js lambda function code??

Feels a bit of a chicken and egg situation to me!

1

1 Answers

4
votes

You can reference your S3 bucket by using a Lambda function environment variable. Your code is static and does not reference a hard-coded bucket by name. Instead, it retrieves the bucket name from the environment variable.

In your CloudFormation script, you would set the environment variable as part of your AWS::Lambda::Function definition under the environment property.

For example:

"Resources" : {

    "MyFunction" : {
      "Type" : "AWS::Lambda::Function",
      "Properties": {
        "Environment" : {
          "Variables" : {
            "BucketName" : { "Ref" : "MyBucket" }
          }
        },
      }
    },
    "MyBucket" : {
       "Type" : "AWS::S3::Bucket"
    }
}