CloudFormation with a code hook maybe using Lambda

0

I know this could be done in Terraform, Ansible and some other things like that. But this question is strictly for CloudFormation.

What I have is a CF Template that our Engineers run to create instances. And while they are supposed to manually pick which AZ to put the instance in often times they do not and the Template takes its default. What I want to know is if there is a way with maybe Lambda or something else in AWS that could do the calculations to see which AZ has the least number of instances in it then tell the CF Template to put the new instance and all its resources in that AZ without human intervention?

The second part of this question is if there is a way to do what I asked how complicated is it compared to doing it in Ansible or Terraform. Both of which would run on an instance inside of AWS with the proper roles to do script calculations and then deployment.

1 Answer
1
Accepted Answer

Hi,

What you want to do can be achieved with a Custom Resource. This is a type of resource in Cloudformation that excutes (and waits for a response from) a Lambda function.

The way it would work in your scenario would be to remove the option of the AZ from the parameters sections completely. Create a Lambda that does the lookup logic you describe and return the AZ. Then trigger this Lambda from a Custom Resource, this custom resource would then have a !Refor !GetAtt value that you can access to populate the AZ value for the instance in place of using a parmeter.

I hope this helps.

If you are likely to create your Lambda in Python then there is a helper library specifically designed to make Custom Resources simpler to create in Python.

profile pictureAWS
danjhd
answered 8 months ago
profile pictureAWS
EXPERT
reviewed 7 months ago
profile picture
EXPERT
reviewed 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions