ZZzzZZzz
ZZzzZZzz

Reputation: 1844

CloudFormation - Access Output of Parent Stack in Child Nested stack

I have a master Cloudformation template which invokes two child templates. I have my first template run and the Outputs captured in the Outputs section of the resource. I have given lot of tries in using the ChildStack01 Output values in the Second Template which is nested and I am not sure why I get Template format error: Unresolved resource dependencies [XYZ] in the Resources block of the template. Here is my master template.

{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
    "LambdaStack": {
        "Type": "AWS::CloudFormation::Stack",
        "Properties": {
            "TemplateURL": "https://s3.amazonaws.com/bucket1/cloudformation/Test1.json",
            "TimeoutInMinutes": "60"
        }
    },
    "PermissionsStack": {
        "Type": "AWS::CloudFormation::Stack",
        "Properties": {
            "TemplateURL": "https://s3.amazonaws.com/bucket1/cloudformation/Test2.json",
            "Parameters": {
                "LambdaTest": {
                    "Fn::GetAtt": ["LambdaStack", "Outputs.LambdaTest"]
                }
            },
            "TimeoutInMinutes": "60"
        }
    }
}
}

Here is my Test1.json Template

{
"Resources": {
    "LambdaTestRes": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Description": "Testing AWS cloud formation",
            "FunctionName": "LambdaTest",
            "Handler": "lambda_handler.lambda_handler",
            "MemorySize": 128,
            "Role": "arn:aws:iam::3423435234235:role/lambda_role",
            "Runtime": "python2.7",
            "Timeout": 300,
            "Code": {
                "S3Bucket": "bucket1",
                "S3Key": "cloudformation/XYZ.zip"
            }
        }
    }
},
"Outputs": {
    "LambdaTest": {
        "Value": {
            "Fn::GetAtt": ["LambdaTestRes", "Arn"]
        }
    }
}
}

Here is My Test2.json which has to use the output of Test1.json.

{
"Resources": {
    "LambdaPermissionLambdaTest": {
        "Type": "AWS::Lambda::Permission",
        "Properties": {
            "Action": "lambda:invokeFunction",
            "FunctionName": {
                "Ref": "LambdaTest"
            },
            "Principal": "apigateway.amazonaws.com",
            "SourceArn": {
                "Fn::Join": ["", ["arn:aws:execute-api:", {
                    "Ref": "AWS::Region"
                }, ":", {
                    "Ref": "AWS::AccountId"
                }, ":", {
                    "Ref": "TestAPI"
                }, "/*"]]
            }
        }
    }
},
"Parameters": {
   "LambdaTest": {
       "Type": "String"
   }
}
}

Upvotes: 0

Views: 3185

Answers (3)

masterforker
masterforker

Reputation: 2527

It is not enough to just have output, you need to export that output. Look here: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-exports.html So you need something like:

"Outputs": {
    "LambdaTest": {
        "Value": {
            "Fn::GetAtt": ["LambdaTestRes", "Arn"]
        }
        "Export": {
            "Name": "LambdaTest"
        }
    }
}

Upvotes: 3

wjordan
wjordan

Reputation: 20390

You have two unresolved Ref resource dependencies in Test2.json, one to LambdaTest and one to TestAPI.

For LambdaTest, it looks like you're trying to pass this as a parameter from the parent stack, but you haven't specified it as an input Parameter in the child Test2.json template. Add an entry in Test2.json's Parameters section, like this:

"Parameters": {
  "LambdaTest": {
    "Type": "String"
  }
},

Regarding TestAPI, this reference doesn't seem to appear anywhere else in your templates, so you should either specify this as a fixed string directly, or add another input Parameter in your Test2.json stack (see above) and then provide it from the parent stack.

Upvotes: 0

Manish Joshi
Manish Joshi

Reputation: 3770

The error is coming from test1.json(LambdaStack).

Logical ID
An identifier for the current output. The logical ID must be alphanumeric (a-z, A-Z, 0-9) and unique within the template.

It seems you have two logical ID with the same name "LambdaTest", one in resource section and other in output section.

Upvotes: 0

Related Questions