How to upload and deploy on Elastic Beanstalk with the aws cli? - amazon-web-services

My Settings:
- I've got a multidocker application specified in my Dockerrun.aws.json file.
- The images of my applications are stored on ECR.
In the AWS console for Elastic Beanstalk, I can "upload and deploy" a new Dockerrun.aws.json file. And then Elastic Beanstalk deploys that version.
Is it possible to do the same ("upload and deploy") via the aws elasticbeanstalk command line?
The closest thing I found was aws elasticbeanstalk rebuild-environment --environment-id $ENVIRONMENT_ID. But that only rebuilds the existing environment with existing Dockerrun.aws.json file. What if I want to deploy my environment with another version of my Dockerrun.aws.json file in the cli?

Yes, you can create a new deployment using the AWS CLI, and as you figured, RebuildEnvironment is not the API call. You are looking for a combination of three calls -- one to S3, and two to Beanstalk
create a zip file of your application code
Upload the zip file to S3. Note the bucket and key names (This would make the new version available to AWS and hence to Beanstalk)
perform a call to ElasticBeanstalk's CreateApplicationVersion API:
aws elasticbeanstalk create-application-version --application-name <beanstalk-app> --version-label <a unique label for this version of code> --description <description of your changes> --source-bundle S3Bucket="<bucket name previously noted",S3Key="<key name previously noted"
perform a call to Beanstalk's UpdateEnvironment API:
aws elasticbeanstalk update-environment --environment-name <name of environment> --version-label <label of app. version created above>
Clearly, this is tedious, so I also suggest you look into deploying through the EBCLI, which does all these things for you through a single command -- eb deploy

Related

How to upload and deploy zip file to AWS elastic beanstalk via CLI?

I do not want to use the console. No manual processes. I need a command line version, something that I can code in my continuous deployment scripts.
As part of the build process, I can output a ZIP file (be it on my local machine or in CI process, e.g: via bitbucket pipelines or AWS codedeploy VM instance).
I want a command like:
aws eb deploy my-app ./server.zip
That is, first upload my chosen zip file and then deploy it (doesn't have to be one command).
The official eb deploy does not seem to support this, nor have I been able to find any other method to do this.
Any ideas would be much appreciated :)
I don't think eb CLI supports uploading a ZIP and updating an environment but you can use a combination of AWS CLI commands.
Upload the ZIP to S3
Create an application version
Update the environment
Example,
aws s3 cp deploy.zip s3://mybucket/deploy.zip
aws elasticbeanstalk create-application-version --application-name my-app --version-label 12345 --source-bundle S3Bucket="mybucket",S3Key="deploy.zip"
aws elasticbeanstalk update-environment --application-name my-app --environment-name MyApp-env --version-label 12345
I was looking for this answer as well. I was able to find some AWS documentation that lets you use the Elastic Beanstalk CLI configuration to upload the zip.
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-configuration.html#eb-cli3-artifact
Deploying an artifact instead of the project folder
You can tell the EB CLI to deploy a ZIP file or WAR file that you generate as part of a separate build process by adding the following lines to .elasticbeanstalk/config.yml in your project folder.
deploy:
artifact: path/to/buildartifact.zip
If you configure the EB CLI in your Git repository, and you don't > commit the artifact to source, use the --staged option to deploy the latest build.
~/eb$ eb deploy --staged
I tested and it did work for me!

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 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.

Elastic Beanstalk CLI upload version without deploying

Is there a way with EB CLI to upload a zip package to the environment but not actually deploy it? This can be done on the web console but I would like to have the output of each build in the CI server to be uploaded to Elastic Beanstalk via EB CLI but only selected builds actually be deployed (this step will be manual in the web console)
The EB CLI does not have the functionality for this process. I would suggest using the AWS CLI to push the local zip into S3 then to upload using the Create Application Version method directly, like this:
aws s3 cp app.zip s3://staging-bucket
aws elasticbeanstalk create-application-version --application-name app-name --version-label deploy-app --source-bundle S3Bucket=staging-bucket,S3Key=app.zip
Does this help your problem?

Is there a way to deploy to Elastic Beanstalk if you are not using git?

I am developing an Elastic Beanstalk app. It is a Scala web application, built with sbt. I want to deploy the resulting WAR from the command line to an existing environment.
All I can find is the eb CLI which appears to require you to use git: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-getting-started.html
Is there not a way to simply specify a WAR and environment name to perform the deployment?
What is the best workaround otherwise? I can upload to S3 from the command line and then use the web app to choose that file, but it's a bit more painful than I wanted.
You can use Elastic Beanstalk CLI (eb) instead of AWS CLI. Just run eb create to create a new environment and eb deploy to update your environment.
You can set specific artifact (your *.war file), by configuring the EB CLI (read: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-configuration.html#eb-cli3-artifact):
You can tell the EB CLI to deploy a ZIP or WAR file that you generate
as part of a separate build process by adding the following lines to
.elasticbeanstalk/config.yml in your project folder.
deploy:
artifact: path/to/buildartifact.zip
I found a way - use the aws CLI instead. First upload to S3 (I actually use s3cmd) then create an application version:
$ aws elasticbeanstalk create-application-version --application-name untaggeddb --version-label myLabel --source-bundle S3Bucket="bucketName",S3Key="key.war"
I believe the application version can then be deployed with update-environment also using the aws CLI.