Reputation: 9
Hi I have a cloudformation stack which creates a couple of ec2 instances. And I'm planing to use aws lambda to ssh into those ec2 instance to manage them.
I need to provide my aws lambda function with the ip address of those ec2 instance. I need to delete the stack and re-run for testing purpose. So the instances' ip address are different every time when I re-run the cloudformation stack. I can't hardcode or set those ip address as environment variable for my aws lambda function.
The stack's name will be the same. And I set up the cloudformation stack to output the ip address of each ec2 instance. Thus, I think I can use the stack name as the reference to let my aws lambda function to access and load its output to achieve this goal.
I found a similar post: AWS lambda read parameter or outputs from CloudFormation saying this.
But he only described "Grant your Lambda function cloudformation:DescribeStacks permission to read outputs of your CloudFormation stack and load this output in your code at runtime".
Could someone provide me more specific steps about how to load in aws lambda? I can't find any aws docs about this so could someone please help me?
Thanks a lot!
Upvotes: 0
Views: 1193
Reputation: 270104
You would use DescribeStacks()
, which will return a list of all stacks. For example, if using Python it would be describe_stacks()
.
Included in the returns information is a list of Outputs
for each stack. You can obtain your desired information from there.
It would be something like:
import boto3
cf_client = boto3.client('cloudformation')
response = cf_client.describe_stacks(StackName='foo')
for output in response['Stacks'][0]['Outputs']:
print(output['OutputKey'], output['OutputValue'])
Upvotes: 1