Created
July 7, 2019 01:47
-
-
Save danp3d/386d7a249c49704601a14345f2d5ce43 to your computer and use it in GitHub Desktop.
Custom CloudFormation Templates - Setting PhysicalResourceId's and checking against !Ref output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
Resources: | |
# lambda function to be used as a custom resource | |
MyLambda: | |
Type: AWS::Lambda::Function | |
Properties: | |
Handler: index.handler | |
Role: !GetAtt BasicExecutionRole.Arn | |
Runtime: python3.7 | |
Timeout: 60 | |
Code: | |
ZipFile: | | |
import cfnresponse | |
def handler(event, context): | |
props = event["ResourceProperties"] | |
logicalId = event["LogicalResourceId"] | |
physicalId = logicalId | |
if "CustomPhysicalId" in props: | |
physicalId = props["CustomPhysicalId"] | |
return cfnresponse.send( | |
event, | |
context, | |
cfnresponse.SUCCESS, | |
responseData={ "LogicalId": logicalId }, | |
physicalResourceId=physicalId | |
) | |
BasicExecutionRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
Action: | |
- 'sts:AssumeRole' | |
ManagedPolicyArns: | |
- arn:aws:iam::aws:policy/AWSLambdaExecute | |
MyCustomResource1: | |
Type: Custom::MyCustomResource | |
Properties: | |
ServiceToken: !GetAtt MyLambda.Arn | |
MyCustomResource2: | |
Type: Custom::MyCustomResource | |
Properties: | |
ServiceToken: !GetAtt MyLambda.Arn | |
CustomPhysicalId: HiMom | |
MyCustomResource3: | |
Type: Custom::MyCustomResource | |
Properties: | |
ServiceToken: !GetAtt MyLambda.Arn | |
CustomPhysicalId: !GetAtt MyLambda.Arn | |
Outputs: | |
MyCustomResource1: | |
Description: Output of the Ref function for MyCustomResource1. Same as PhysicalResourceId | |
Value: !Ref MyCustomResource1 | |
MyCustomResource2: | |
Description: Output of the Ref function for MyCustomResource2. Same as PhysicalResourceId | |
Value: !Ref MyCustomResource2 | |
MyCustomResource3: | |
Description: Output of the Ref function for MyCustomResource3. Same as PhysicalResourceId | |
Value: !Ref MyCustomResource3 |
So, the three tests show that the Physical ID corresponds to the output of Ref, right?
Exactly :)
This is not documented anywhere, but I haven't seen a single case so far where this is not true - looks like the !Ref function always returns the PhysicalResourceId
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, the three tests show that the Physical ID corresponds to the output of Ref, right?