This script is executed by the template to perform the application(s) deployment based on `aws` CLI, and uses dynamic variables provided by the template.
This script is executed by the template to perform the application(s) deployment based on `aws` CLI, and uses [dynamic variables](#dynamic-variables) provided by the template (`${appname}` is used as the CloudFormation stack name).
It implements [dynamic environment URLs](#static-vs-dynamic-environment-urls), by generating the `environment_url.txt` file, containing
the dynamically generated url at the end of the deployment script.
```bash
#!/usr/bin/env bash
set-e# fail on error
echo"[aws-deploy] Deploying $appname..."
# disable AWS CLI pager
export AWS_PAGER=""
template_file=file://MyStack.template
# retrieve environment type ($env) from template
# retrieve $AWS_KEYPAIR_NAME from project secret variables
This chapter gives some implementation hints to implement continuous deployment of a Serverless application based on [Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/).
It enables review, staging and production environments.
#### `.gitlab-ci.yml`
```yaml
include:
# Include AWS template
-project:'to-be-continuous/aws'
ref:'1.0.1'
file:'/templates/gitlab-ci-aws.yml'
...
# Global variables
variables:
# AWS
# use an image with both aws and sam CLI
AWS_CLI_IMAGE:"pahud/aws-sam-cli:latest"
# AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY defined as secret CI/CD variable
AWS_REVIEW_ENABLED:"true"# enable review env
AWS_STAGING_ENABLED:"true"# enable staging env
AWS_PROD_ENABLED:"true"# enable production env
...
# Pipeline steps
stages:
-build
-test
-deploy
-acceptance
-publish
-production
```
#### AWS scripts
##### `aws-deploy.sh`
This script is executed by the template to perform the application(s) deployment based on `sam`and `aws` CLI, and uses [dynamic variables](#dynamic-variables) provided by the template (`${appname}` is used as the SAM/CloudFormation stack name).
It implements [dynamic environment URLs](#static-vs-dynamic-environment-urls), by generating the `environment_url.txt` file, containing
the dynamically generated url at the end of the deployment script.
```bash
#!/usr/bin/env bash
set-e# fail on error
echo"[aws-deploy] Deploy $appname..."
# disable AWS CLI pager
export AWS_PAGER=""
# 1: build
sam build ${TRACE+--debug}
# 2: deploy (each environment is a separate stack)
# AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY defined as secret CI/CD variable
# AWS_DEFAULT_REGION and AWS_SAM_BUCKET defined as project CI/CD variable
sam deploy ${TRACE+--debug}\
--stack-name"$appname"\
--region"$AWS_DEFAULT_REGION"\
--s3-bucket"$AWS_SAM_BUCKET"\
--no-fail-on-empty-changeset\
--no-confirm-changeset\
--tags"ci-job-url=$CI_JOB_URL environment=$env"
# Retrieve outputs (use cloudformation query)
api_url=$(aws cloudformation describe-stacks --stack-name"$appname"--output text --query'Stacks[0].Outputs[?OutputKey==`MyProjectApiUrl`].OutputValue')
echo"Stack created/updated:"
echo" - Api URL: $api_url"
# Finally set the dynamically generated WebServer Url
echo"$api_url"> environment_url.txt
```
##### `aws-cleanup.sh`
This script is executed by the template to perform the application(s) cleanup based on `aws` CLI (review env only).