2
votes

I want to send a notification with the data output from my cloudformation stack after it has completed using a lambda function that sends the data to Slack.

How can I access the cloudformation output data from the SNS message?

1
You can't get outputs from SNS message. You have to get the stack name/arn and use AWS SDK to query the stack for the outputs.Marcin
How do i get the stack arn?? can I get it inside the lambda? because I create new stacks all the timeWalksAway

1 Answers

1
votes

The SNS message that CloudFormation produces has the following form:

{
    "Records": [
        {
            "EventSource": "aws:sns",
            "EventVersion": "1.0",
            "EventSubscriptionArn": "arn:aws:sns:us-east-1:761323155761:my-topic:6eaf0af0-8707-45b8-a7fb-c900c56e0ca0",
            "Sns": {
                "Type": "Notification",
                "MessageId": "a681c82f-9a58-58a2-bc5a-9fc379857c94",
                "TopicArn": "arn:aws:sns:us-east-1:761323155761:my-topic",
                "Subject": "AWS CloudFormation Notification",
                "Message": "StackId='arn:aws:cloudformation:us-east-1:761323155761:stack/fggg/1294f6d0-0225-11eb-93fc-127c6c2d7f71'\nTimestamp='2020-09-29T07:26:21.471Z'\nEventId='129740c0-0225-11eb-93fc-127c6c2d7f71'\nLogicalResourceId='fggg'\nNamespace='761323155761'\nPhysicalResourceId='arn:aws:cloudformation:us-east-1:761323155761:stack/fggg/1294f6d0-0225-11eb-93fc-127c6c2d7f71'\nPrincipalId='AIDA3CQTFOEYUR5EGW2C5'\nResourceProperties='null'\nResourceStatus='CREATE_IN_PROGRESS'\nResourceStatusReason='User Initiated'\nResourceType='AWS::CloudFormation::Stack'\nStackName='fggg'\nClientRequestToken='Console-CreateStack-5414c3bd-914a-5eec-7b6e-1b0e9139cd20'\n",
                "Timestamp": "2020-09-29T07:26:21.560Z",
                "SignatureVersion": "1",
                "Signature": "pE/h7Euukmm4xD/xedKlqN6pFDmWifx5GxdPHnK1pB+RVZvY60rO2XUBYTB11LNbDhaSxt7z6yyOWBHd29bMRtAeOOpl4wHzi3gYbrZbA//8xCdmoRHS2BRq4HMpT8WTxr6/i1dCrIjjyeT5KRGBZ/7WxvKJBixtHt+hjy9u41KUOL2HeHriiE+HYyqMCpzLb9DLY13jwpjtuwlk+e5le5rFmW6SobFLodhWDfers9wH760A5j3CxFt8uQnZmkInpega+SD3rqU9Ue/ZZAxxe2XDMsWT6EgznEJF/ToTbTo6HtNmiXE5iNd5xZAEjfPLPQ1Bc3R+JuvuWuiqJXkzCg==",
                "SigningCertUrl": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-a86cb10b4e1f29c941702d737128f7b6.pem",
                "UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:761323155761:my-topic:6eaf0af0-8707-45b8-a7fb-c900c56e0ca0",
                "MessageAttributes": {}
            }
        }
    ]
}

As you can see above, it contains Message field with StackId. Thus when your function gets this message from SNS, you have to parse it to get the StackId, and then use AWS SDK to describe-stacks. The outcome of the SDK call will contain the stack's outputs.