Skip to content

Instantly share code, notes, and snippets.

@danp3d
Created July 7, 2019 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danp3d/386d7a249c49704601a14345f2d5ce43 to your computer and use it in GitHub Desktop.
Save danp3d/386d7a249c49704601a14345f2d5ce43 to your computer and use it in GitHub Desktop.
Custom CloudFormation Templates - Setting PhysicalResourceId's and checking against !Ref output
---
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
@cpmech
Copy link

cpmech commented Jul 7, 2019

So, the three tests show that the Physical ID corresponds to the output of Ref, right?

@danp3d
Copy link
Author

danp3d commented Jul 7, 2019

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