0

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!

2
  • Are you running the cloudformation CLI as part of an automated pipeline or manually? Commented Jul 16, 2020 at 0:14
  • If you need to run the lambda functions directly after the creation of the instances, you could implement them as a custom resource and pass the instances a !Refs in the event Commented Jul 16, 2020 at 3:05

1 Answer 1

1

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']) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.