Reputation: 1521
I have created a L1 lambda layer with the aws cdk as indicated in the docs. I would like to attach this lambda layer to a lambda function. Documentation on lambda functions says the following about the parameter layer:
layers (Optional[Sequence[str]]) – A list of function layers to add to the function’s execution environment. Specify each layer by its ARN, including the version.
How can I get the ARN of my lambda layer in L1? I tried
cfn_function = lambda_.CfnFunction(self, ..., layers = [cfn_layer_version.get_att(resource.arn)])
Upvotes: 0
Views: 2446
Reputation: 1521
We must keep in mind that when we are working with L1 constructs in the CDK, these constructs directly represent all resources available in AWS CloudFormation [1]. Essentially, defining the configuration for a resource using an L1 construct would be the same as defining the resource in a CloudFormation template directly. With this being said, if we would like to obtain the ARN for an L1 construct, we can use the ".ref" attribute for a given construct that we have defined.
For example, if we are to define a Lambda Layer as follows:
cfn_layer_version = lambda_.CfnLayerVersion(self, "MyCfnLayerVersion",
content=lambda_.CfnLayerVersion.ContentProperty(
s3_bucket="my-bucket-name-here",
s3_key="module.zip"
)
)
I can create a CloudFormation output where the value is the ARN of my Layer using "ref" [2] as follows:
cdk.CfnOutput(self, "LayerARN", value= cfn_layer_version.ref)
Another way we can get the output is to use the "get_att()" method of the resource that we have created as follows:
cdk.CfnOutput(self, "LayerARN", value= cfn_layer_version.get_att('Ref').to_string())
As you can see, we look for the attribute "Ref" which aligns with the AWS::Lambda::LayerVersion documentation [3].
Lastly, I do recommend that you work with the higher level constructs where you can in the CDK as they provide mode flexibility when writing up your CDK code. For instance, you can use the following high level construct to create a Lambda Layer in the CDK [4].
[1] Constructs - AWS Construct library - https://docs.aws.amazon.com/cdk/v1/guide/constructs.html#constructs_lib [2] CfnLambdaLayer ref: https://docs.aws.amazon.com/cdk/api/v1/python/aws_cdk.aws_lambda/CfnLayerVersion.html#aws_cdk.aws_lambda.CfnLayerVersion.ref [3] AWS::Lambda::LayerVersion - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-layerversion.html [4] LambdaLayer: https://docs.aws.amazon.com/cdk/api/v1/python/aws_cdk.aws_lambda/LayerVersion.html
Upvotes: 0
Reputation: 11512
You would use cfn_layer_version.ref
to get the ARN.
From Cloudformation docs:
When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the ARN of the layer version, such as arn:aws:lambda:us-west-2:123456789012:layer:my-layer:1.
Upvotes: 1