Attach ASG from Beanstalk to TargetGroup - amazon-web-services

I have a CloudFormation template that creates an AWS::ElasticBeanstalk::Environment and an AWS::ElasticLoadBalancingV2::TargetGroup
I would like to associate the AutoScalingGroup that beanstalk creates with the TargetGroup created in my template.
My end goal is doing path-based routing via an ALB to a bunch of beanstalk applications from a single domain (i.e., www.domain.com/foo routes to ebapp1 and www.domain.com/bar routes to ebapp2)
I can actually accomplish what I wish via the CLI:
aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name "<asg-name>" --target-group-arns "<arn-for-target-group>"
However, I would like to have this association created automatically when I launch my new beanstalk environment via CloudFormation.
I am having trouble figuring out how to translate this into my CloudFormation template. Any pointers?

Your CLI command is related to AWS::AutoScaling::AutoScalingGroup, and if you want to get result as same as your CLI command you can use TargetGroupARNs parameter on AWS::AutoScaling::AutoScalingGroup.
Use the Reference function to get the value of target group arn parameter.
If this is not what you need, and if your command really works, you can call it using UserData or metadata in an instance that you have created on your template to run your CLI command.

Related

How do I specify a CodeDeploy application and deployment group name in SAM template?

I'm using a SAM template for B/G deployment. The problem is a CodeDeploy resource is automatically created. Is it possible to specify a name for application and deployment group?
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/automating-updates-to-serverless-apps.html
If you enable gradual deployments through your AWS SAM template, a
CodeDeploy resource is automatically created for you. You can view the
CodeDeploy resource directly through the AWS Management Console.

AWS codedeploy blue green deployment

I have setup code pipeline for end to end automatic deployment of revision on EC2 instances using cloudformation template, the deployment group is of type blue/green for codedploy.
But I dont understand how to keep the code deployment group in sync with newly created auto scaling group (green).
Do I have to create new lambda invoke action in pipeline after successful deployment to update the newly created auto scaling group name.
Unfortunately, CloudFormation does not support Blue/Green deployments for EC2 platform:
For blue/green deployments, AWS CloudFormation supports deployments on Lambda compute platforms only.
Support for ECS is very new.
To create deployment group for blue/green for EC2 platform you would have to create a custom resource in CloudFormation .
The custom resource would be based on a lambda function, and in that lambda function you would use create_deployment_group to define blue/green details for your EC2 instances. As part of this process, you will have an option to choose how to deal with AutoScaling group, e.g.
"greenFleetProvisioningOption": {
"action": "COPY_AUTO_SCALING_GROUP"
}
For creation of custom resource, crhelper by AWS is very useful.
Hope this helps and hope Blue/Green for EC2 will be supported by CloudFormation soon.

aws cloud formation idenitfy ami id based on tags and create instance

I have a usecase where the cloud formation has to identify AMI-ID based on tags (passed as inputs to cloud formation template) and likewise create an ec2 instance.
Can I run a script to identify (aws ec2 describe-images --filters "Name=tag:Name,Values=value1" --query 'Images[*].{ID:ImageId}' --output text) AMI-ID via cloud formation and pass it for ec2 instance creation..
Any thoughts or example on how to do this ?
The suggested method by Amazon is provided here: Walkthrough: Looking Up Amazon Machine Image IDs
Summary: you create a custom resource in your CloudFormation template that queries a Lambda function. The Lambda function runs the AMI querying code and returns that to the CloudFormation template.
An alternative method is provided here: Integrating AWS CloudFormation with AWS Systems Manager Parameter Store
In this second method, you use CloudFormation to lookup values in AWS Parameter Store. How you decide what values to put into Parameter store is a different issue.
You cannot run the script as part of your CloudFormation template.
You will need to execute the command before creating the CloudFormation stack to determine the AMI ID. Then pass the final AMI ID into the CloudFormation stack by way of an input parameter.
So it will be a 2-step process. But you can create a script to do it.

Cloudformation ECS cluster

I am attempting to write a Cloud Formation Template that will create a launch configuration, ASG, ECS cluster, and register the ec2 instances in the ASG to said cluster.
I have a long userdata.sh file that is used to setup a launch configuration.
By putting this userdata in the yaml file
i.e.
...
UserData:
Fn::Base64: !Sub |
#!/bin/bash
...
it looks pretty terrible. Also, when i try and plug in the cluster name into the ecs agent ${ECSCluster} with Sub, i get "variable names in Fn::Sub syntax must contain only alphanumeric characters, underscores, periods, and colons". The environment variables in my userdata take on the same form ${ENV_VARIABLE}.
I've tried Passing userdata file to AWS Cloudformation stack but receive "Template format error: YAML not well-formed. "
What is the recommended way for passing in a complex userdata.sh file into a CFT in such a way that you can add the cluster name to the ecs-agent config?
Error is just stating that it doesn't like the way you reference your variable in Fn::Sub. Instead of $(ECSCluster) it needs to be ${ECSCluster}. See the examples in the docs.

Cloudformation not launching WAF stack in required region

I am trying to launch a stack via AWS CloudFormation using these provided sample stack(s) for WAF:
https://s3.amazonaws.com/solutions-reference/aws-waf-security-automations/latest/aws-waf-security-automations.template
https://s3.amazonaws.com/cloudformation-examples/community/common-attacks.json
I want to launch these stacks in us-east-1 region and I am doing so by going to this URL :
https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/new
Still when the WAF ACL and rules are created they are part of Global (Cloudfront) instead of us-east-1 region. I have tried this a few times and it works like this.
To launch these stacks in a specific region do I need to do some thing more?
AWS now provides two CloudFormation templates. One for CloudFront (Global) and the other for ELB (Regional).
https://aws.amazon.com/answers/security/aws-waf-security-automations/