Reputation: 4853
I have a Cloudformation stack with a master template and two nested templates, one of which contains a Lambda and associated role, another which contains an S3 bucket. I want to export the Lambda ARN from the Lambda template and pass it to the S3 template so I can do some event notification stuff (have a new item in the S3 bucket trigger the Lambda).
If I comment out the CF code in the master template which imports the exported Lambda ARN and passes it to the bucket, everything works fine - the master and nested templates deploy fine, and I can see the exported Lambda ARN -
(c4abb639548f2545e145ea54ed13d2ea) justin@justin-XPS-13-9360:~/work/gists/c4abb639548f2545e145ea54ed13d2ea$ ./describe_outputs.sh xxx-MyLambda-1B451J61821EC
------------------------------------------------------------------------------------------------------------------
| DescribeStacks |
+-------------------------+--------------+-----------------------------------------------------------------------+
| ExportName | OutputKey | OutputValue |
+-------------------------+--------------+-----------------------------------------------------------------------+
| xxx-lambda-arn| FunctionArn | arn:aws:lambda:eu-west-1:119552584133:function:xxx-lambda |
+-------------------------+--------------+-----------------------------------------------------------------------+
However if I uncomment the code passing the exported Lambda ARN to the bucket template -
MyBucket:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: !Sub "https://s3.${AWS::Region}.amazonaws.com/${S3Bucket}/${AppName}/templates/bucket.yaml"
Parameters:
BucketName: !Join
- '-'
- - Ref: AppName
- bucket
LambdaArn: # <- this
Fn::ImportValue: # <- this
Fn::Sub: "${AppName}-lambda-arn" # <- this
then suddenly deployment of the entire stacks fails -
(c4abb639548f2545e145ea54ed13d2ea) justin@justin-XPS-13-9360:~/work/gists/c4abb639548f2545e145ea54ed13d2ea$ aws cloudformation describe-stack-events --stack-name xxx
{...}
ROLLBACK_IN_PROGRESS | No export named xxx-lambda-arn found. Rollback requested by user.
I'm surprised the main stack isn't able to detect that one of its nested stacks (the S3 stack) needs a value to be exported from the other nested stack (the Lambda stack).
I hoped this might be resolvable by adding a DependsOn
block, making the S3 nested stack dependent on the Lambda nested stack, but this doesn't work either.
Is there some kind of restriction on importing/exporting values across nested stack belonging to the same master stack ? Or is there a workaround ?
Full gist here -
https://gist.github.com/jhw/c4abb639548f2545e145ea54ed13d2ea
Upvotes: 4
Views: 15475
Reputation: 2321
In your nested stack, you need to specify an output so the master stack has access to it from the resource i.e.
"Resources": {
"LambdaFunction": ...
}
"Outputs": {
"LambdaFunction": {
"Value":
{ "Fn::GetAtt": ["LambdaFunction","Arn"] }
}
LambdaArn: !GetAtt Lambda.Outputs.LambdaArn
The Lambda can be referenced from the master stack for more information take a look at this reference architecture https://github.com/aws-samples/ecs-refarch-cloudformation.
Upvotes: 5