Is it possible to replace AMI programmatically in AWS ElasticBeanstalk? - amazon-web-services

From the console, I am able to replace the AMI under Configuration > Modify Instances > AMI ID
I'm trying to do this programmatically using AWS CLI but couldn't find any API to do this.
I tried to replace AMI ID from Cloudformation (that EB deploys) to replace ASG and LC, but then EB environment broke.

I found it, using this command:
aws elasticbeanstalk update-environment --application-name $EBAppName --environment-name $EBEnvName --option-settings Namespace=aws:autoscaling:launchconfiguration,OptionName=ImageId,Value=$ImageID

Related

How to find the created date of the AWS Lightsail Instance?

I have like 15 Lightsail instances created on my AWS account and now I wanted to know when these Lightsail instances were created.
The creation date of the Lightsail instances on which date and time these were created. But is not able to find this information from the AWS Lightsail console.
You can use the AWS Command Line Interface (AWS CLI) to retrieve the creation date of your Lightsail instances. The following AWS CLI command will list all of your Lightsail instances and the creation date for each instance:
aws lightsail get-instances --query "instances[*].{Name:name, CreationDate:createdAt}"
This command uses the get-instances command to retrieve information about all of your Lightsail instances, and the --query option to extract the name and creation date of each instance.
Alternatively, you can also retrieve the creation date of a specific Lightsail instance by using the get-instance command and specifying the instance name:
aws lightsail get-instance --instance-name <instance_name> --query "instance.createdAt"
Replace <instance_name> with the name of the Lightsail instance you want to retrieve information for.
Note: The AWS CLI must be installed and configured on your local machine in order to use these commands.
If you haven't already done so, just follow these steps:
1- Install the AWS CLI:
You can install the AWS CLI on your local machine by following the installation instructions for your operating system. You can find the installation instructions at the following URL: https://aws.amazon.com/cli/
2- Configure the AWS CLI:
After installing the AWS CLI, you need to configure it with your AWS credentials. You can do this by running the following command:
aws configure
This will prompt you for your AWS access key ID, secret access key, default region name, and default output format. You can find your AWS access keys in the AWS Management Console.
3- Verify the configuration:
To verify that your AWS CLI is configured correctly, you can run the following command:
aws lightsail get-instances
This command should list all of your Lightsail instances in your AWS account.
With the AWS CLI installed and configured, you can now use the AWS CLI commands to retrieve information about your Lightsail instances, including the creation date.
I hope my answer helps you

Use CLI to Deploy Code to Existing Amazon EB Environment

I've created an AWS Elastic Beanstalk application and environment. However, I cannot for the life of me figure out how to deploy code to it. Most tutorials I read are for creating a new application directly from the CLI, but I already have one.
I've installed the AWS CLI tools. I created a SSH key-pair to the environment and added it to my .ssh folder. I created an IAM profile and logged in with that in my terminal.
If I understand correctly, I need to do eb use [my environment name] so I can then eb deploy to it. But when I use eb list, nothing comes up. How can I connect to the environment that already exists on AWS?
I am using Linux (WSL on Windows). I'm also on the Free Tier of AWS.
You can either use AWS CLI commands or EB CLI commands to deploy your applications.
Congfigure the source, GIT or S3. e.g. I have uploaded my_app.zip to my_bucket in S3.
Create a new application version. It is a good practise to use commit hash as version label.
aws elasticbeanstalk create-application-version
--application-name <EB_APP_NAME> --version-label <version-label>
--source-bundle S3Bucket="my_bucket",S3Key=my_app.zip --auto-create-application
Update the environment to point to the new application version. The value of version-label should be the same as in the previous step.
aws elasticbeanstalk update-environment
--application-name <EB_APP_NAME>
--environment-name <EB_ENV_NAME>
--version-label <version-label>
The alternative way is to use EB CLI. eb deploy handles all 3 steps above.
Initialize EB CLI using eb init.
Deploy using eb deploy.

How to use output from one AWS CLI command as input to other

I am running an AWS CLI command to get the latest Auto-scaling Group name in a given account, now I want to use the output (i.e. ASG Name) as an input to other AWS command to update that ASG.
Command to get the ASG name:
aws autoscaling describe-auto-scaling-groups --query "sort_by(AutoScalingGroups,&CreatedTime)[-1].{id:AutoScalingGroupName}" --output text
Command to update the ASG returned by above command:
aws autoscaling update-auto-scaling-group --auto-scaling-group-name $NameReturnedByAboveCommand
I tried these solutions but I am not able to get it to work:
https://stackoverflow.com/a/38153936/2858235
https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-output.html#controlling-output-format
I am using windows command but also tried using PS but no luck.

How to deploy .war from s3 to elasticbeanstalk using AWS CLI

I'm using aws cli (not eb cli).
I have copied a .war to my s3 bucket:
aws s3 cp app-${VERSION}.war s3://elasticbeanstalk-xxx/sub/app-${VERSION}.war
Now I have for example: app-1.0.0.war on my S3. But now I'm stuck.
I have an elastic beanstalk application named superapp. I have 2 environments superapp-dev and superapp-uat.
Now I try to use something like this:
aws elasticbeanstalk update-environment --application-name superapp --environment-name superapp-dev --version-label 1.0.0'
But this does not work.
I assume I first have to add a the version to my .war and after that I can update my environment?
Can someone help me to explain what step(s) I'm missing?
After upload your file to S3, you must create the "label" on Elastic Beanstalk side with a call to create-application-version.
In your case :
aws elasticbeanstalk create-application-version --application-name superapp --version-label 1.0.0 --source-bundle S3Bucket=elasticbeanstalk-xxx,S3Key=sub/app-1.0.0.war
See complete reference here for more optional parameters: https://docs.aws.amazon.com/cli/latest/reference/elasticbeanstalk/create-application-version.html
After that, you will be able to do your update-environment.

aws ec2 import-image via Terraform

I'm searching for the Terraform equivalent of "aws ec2 import-image --description "Windows 2008 OVA" --license-type --disk-containers file://containers.json" but cannot find a matching resource command in the documentation.
The purpose is to lift an OVA image out of S3 and covert to AMI so it can be used to launch EC2 instances.
There is an option in packer where you can create OVA by a builder and upload it to S3 and then create an AMI. Once you have the AMI, you can inject into your terraform script to launch it.
https://www.packer.io/docs/post-processors/amazon-import.html
https://github.com/terraform-providers/terraform-provider-aws/issues/5998
Right now terraform not supporting for ec2 image import