Run aws cli command in CodeBuild with environment variable substituion - amazon-web-services

I am trying to run an aws cli command at the end of a CodeBuild from the buildspec.yml.
The container/image is "aws/codebuild/amazonlinux2-x86_64-standard:1.0"
I have an environment variable of $Branch (currently set to 'master')
and I want to run the command "aws codepipeline start-pipeline-execution --name bbentityinterface-master-Pipeline"
I have tried "aws codepipeline start-pipeline-execution --name $(bbentityinterface-$Branch-Pipeline)"
and "aws codepipeline start-pipeline-execution --name bbentityinterface-$Branch-Pipeline"
and both fail.
"aws codepipeline start-pipeline-execution --name $(bbentityinterface-$Branch-Pipeline)" fails as below (from the log):
How can I properly construct this line to execute the command?
Running command aws codepipeline start-pipeline-execution --name $(bbentityinterface-$Branch-Pipeline)
/codebuild/output/tmp/script.sh: line 4: bbentityinterface-master-Pipeline: command not found
usage: aws [options] [ ...] [parameters]
To see help text, you can run:
aws help
aws help
aws help
aws: error: argument --name: expected one argument
[Container] 2020/01/08 15:46:40 Command did not exit successfully aws codepipeline start-pipeline-execution --name $(bbentityinterface-$Branch-Pipeline) exit status 2

Figured it out...
eval "aws codepipeline start-pipeline-execution --name bbentityinterface-$Branch

Related

Amazon cli image doesn't work with Gitlab CI

I'm trying to run the aws command (to invalidate cloudfront) using official aws image and have the following config of .gitlab-ci.yml:
static-invalidation:
<<: *production-env
stage: static_invalidation
image: amazon/aws-cli:latest
variables:
AWS_ACCESS_KEY_ID: $CLOUDFRONT_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $CLOUDFRONT_AWS_SECRET_ACCESS_KEY
script:
- aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_DISTRIBUTION --invalidation-batch file://deployment/configs/inv-batch.json
The job fails with the following error:
Using docker image sha256:00cf4f100b03d1b26e93cce377c1311c34efa753e379cd5c6ea5d458337cbaab for amazon/aws-cli:2.2.17 with digest amazon/aws-cli#sha256:39e9898fc43f618636a2190f82b9babcdc618d054e66b49c9959b9cd23285ade ...
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument command: Invalid choice, valid choices are:
accessanalyzer | acm
acm-pca | alexaforbusiness
...
ERROR: Job failed: exit code 252
Any ideas why it's not working?
When I run the command locally it works.
amazon/aws-cli docker has set entrypoint to aws as it's expected to be executed as
$ docker run amazon/aws-cli cloudfront create-invalidation --distribution-id ...
but gitlab expects entry point to shell so it can then execute script commands.
Check out gitlab documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#available-settings-for-image
You will find there how to override docker image entrypoint.
Working config is:
image:
name: amazon/aws-cli:2.2.18
entrypoint: [""]

How to see the deployment progress via aws cli

I created a deployment using AWS cli:
aws deploy create-deployment --application-name systest1 --deployment-group-name TEST --s3-location bucket=artifacts,bundleType=zip,key=APP.zip
and I got the out as
{
"deploymentId": "d-559F8S41O"
}
How can I see the status of the deployment?? I believe
aws deploy get-deployment
will show us the status but it will not show continuous progress. Actually, I am using AWS cli so use don't have to go on AWS console and check the deployment status instead he will get the result on gitlab-ci output only. Any suggestion??
Use the below CLI command to get the status:
aws deploy get-deployment --deployment-id d-52EZVE6PC --query "deploymentInfo.[status, creator]" --output text

AWS sam deploy return error invalid choice

I'm following the instruction here to use AWS CodeDeploy to push code from GitHub to AWS.
I run into this error:
$ sam deploy -template-file packaged.yaml –stack-name mySafeDeployStack –capabilities CAPABILITY_IAM
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument subcommand: Invalid choice, valid choices are:
push | register
deregister | install
uninstall
I have previously run this command successfully:
$ sam package --template-file template.yaml --s3-bucket my-bucket --output-template-file packaged.yaml
Uploading to ... (100.00%)
Successfully packaged artifacts and wrote output template to file packaged.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file .../packaged.yaml --stack-name <YOUR STACK NAME>
$ sam --version
SAM CLI, version 0.6.0
I've tried the recommended command:
aws cloudformation deploy ...
but it returns the same error.
It looks like you're using single dashes for the flags when they require two. The sam package command succeeded since you used two dashes for it.
This should work:
sam deploy --template-file packaged.yaml --stack-name mySafeDeployStack --capabilities CAPABILITY_IAM

AWS CLI - CodeDeploy Create-Deployment has Unknown Options error

I have a normal CodeDeploy package which is able to deployed using the Console without any issues, but when using the CLI, I get a weird error.
aws deploy create-deployment --application-name MyApp --s3-location bucket=mybucket,key=My.Zip.1.1.1.zip,bundleType=zip --deployment-group-name MyDeploymentGroup --deployment-config-name CodeDeployDefault.OneAtATime --description "This throwns an error"
Running the above, I get the following returned:
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
Unknown options: bundleType=zip, key=My.Zip.1.1.1.zip
Weird, since the help does say you need bundleType and key in the s3-location parameter. Just as a test, I remove these unknown options:
aws deploy create-deployment --application-name MyApp --s3-location bucket=mybucket --deployment-group-name MyDeploymentGroup --deployment-config-name CodeDeployDefault.OneAtATime --description "This throwns an error"
Which returns:
--s3-location must specify bucket, key and bundleType.
So, now you know of these options and require them? What is going on here? Am I missing something obvious?
When I ran the code you provided, I got the following returned:
$ aws --version
aws-cli/1.10.60 Python/2.7.11 Darwin/15.6.0 botocore/1.4.50
$ aws deploy create-deployment --application-name ...snip
An error occurred (ApplicationDoesNotExistException) when calling the CreateDeployment operation: No application found for name: MyApp
And this(AWS server side error) is the expected behaviour(I guess).
One possibility that your CLI raised the validation error is that your AWS CLI is out of date.
Which version is your AWS CLI?
I had the same issue. It seemed to be related to the version of PowerShell. It worked on v5, but not on v4.
To fix it, I just put double quotes around the s3-location argument:
aws deploy create-deployment --application-name MyApp --s3-location "bucket=mybucket,key=My.Zip.1.1.1.zip,bundleType=zip" --deployment-group-name MyDeploymentGroup --deployment-config-name CodeDeployDefault.OneAtATime --description "This throwns an error"

AWS CodeDeploy - Error deploying - ApplicationDoesNotExistException

I want to deploy a project in AWS using :
$ aws --region eu-central-1 deploy push --application-name DemoApp --s3-location s3://paquirrin-codedeploy/Project1.zip --ignore-hidden-file --source .
But I got this error:
A client error (ApplicationDoesNotExistException) occurred when calling the RegisterApplicationRevision operation: Applications not found for 289558260222
but the application exists:
$ aws deploy list-applications
{
"applications": [
"DemoApp"
]
}
and CodeDeploy agent is running
[root#ip-171-33-54-212 ~]# /etc/init.d/codedeploy-agent status
The AWS CodeDeploy agent is running as PID 2649
but I haven't found the folder deployment-root inside /opt/codedeploy-agent !
You are deploying to region eu-central-1. But you may not be listing the applications in eu-central-1 using following command:
aws deploy list-applications
Instead, use following command to ensure that application exists:
aws deploy list-applications --region eu-central-1