Skip to content

Commit 677bda6

Browse files
committed
Ensure at least one key/secret is defined per the AWS cli order of
operations instead of assuming in a specific location.
1 parent 26967fa commit 677bda6

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

README.md

+27-23
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,32 @@ Status | In Progress: 0 | Pending: 0 | Skipped: 0 | Succeeded: 1 | Failed:
9595
* The file can then be executed from the /vendor/bin directory: `bash vendor/bin/aws-code-deploy.sh`
9696

9797

98-
## Variables
98+
## Environment Variables
99+
100+
Brief summary is listed in the table below. Full descriptions with recommendations can be found by searching
101+
the readme for the variable name.
102+
103+
| Variable | Required | Description |
104+
| :---------------------------------------- | :------- | :----------------------------------------------------------|
105+
| `AWS_CODE_DEPLOY_KEY` | No | If specified, sets the AWS key id |
106+
| `AWS_CODE_DEPLOY_SECRET` | No | If specified, sets the AWS secret key |
107+
| `AWS_CODE_DEPLOY_REGION` | No | If specified, sets the AWS region |
108+
| `AWS_CODE_DEPLOY_APPLICATION_NAME` | **Yes** | Application name. If it does not exist, will create. |
109+
| `AWS_CODE_DEPLOY_DEPLOYMENT_GROUP_NAME` | **Yes** | Deployment group name. If it does not exist, will create. |
110+
| `AWS_CODE_DEPLOY_DEPLOYMENT_CONFIG_NAME` | No | Deployment config name. By default: _CodeDeployDefault.OneAtATime_ |
111+
| `AWS_CODE_DEPLOY_MINIMUM_HEALTHY_HOSTS` | No | The minimum number of healthy instances during deployment. By default: _type=FLEET_PERCENT,value=75_ |
112+
| `AWS_CODE_DEPLOY_SERVICE_ROLE_ARN` | No | Service role arn giving permissions to use Code Deploy when creating a deployment group |
113+
| `AWS_CODE_DEPLOY_EC2_TAG_FILTERS` | No | EC2 tags to filter on when creating a deployment group |
114+
| `AWS_CODE_DEPLOY_AUTO_SCALING_GROUPS` | No | Auto Scaling groups when creating a deployment group |
115+
| `AWS_CODE_DEPLOY_APP_SOURCE` | **Yes** | The source directory used to create the deploy archive |
116+
| `AWS_CODE_DEPLOY_S3_BUCKET` | **Yes** | The name of the S3 bucket to deploy the revision |
117+
| `AWS_CODE_DEPLOY_S3_KEY_PREFIX` | No | A prefix to use for the revision bucket key |
118+
| `AWS_CODE_DEPLOY_S3_FILENAME` | **Yes** | The destination name within S3. |
119+
| `AWS_CODE_DEPLOY_S3_LIMIT_BUCKET_FILES` | No | Number of revisions to limit. If 0, unlimited. By default: 0 |
120+
| `AWS_CODE_DEPLOY_S3_SSE` | No | If specified and `true` will ensure the CodeDeploy archive is stored in S3 with Server Side Encryption (SSE) |
121+
| `AWS_CODE_DEPLOY_REVISION_DESCRIPTION` | No | A description that is stored within AWS Code Deploy that stores information about the specific revision |
122+
| `AWS_CODE_DEPLOY_DEPLOYMENT_DESCRIPTION` | No | A description that is stored within AWS Code Deploy that stores information about the specific deployment |
99123

100-
```
101-
AWS_CODE_DEPLOY_KEY
102-
AWS_CODE_DEPLOY_SECRET
103-
AWS_CODE_DEPLOY_REGION
104-
AWS_CODE_DEPLOY_APPLICATION_NAME
105-
AWS_CODE_DEPLOY_DEPLOYMENT_CONFIG_NAME
106-
AWS_CODE_DEPLOY_MINIMUM_HEALTHY_HOSTS
107-
AWS_CODE_DEPLOY_DEPLOYMENT_GROUP_NAME
108-
AWS_CODE_DEPLOY_SERVICE_ROLE_ARN
109-
AWS_CODE_DEPLOY_EC2_TAG_FILTERS
110-
AWS_CODE_DEPLOY_AUTO_SCALING_GROUPS
111-
AWS_CODE_DEPLOY_APP_SOURCE
112-
AWS_CODE_DEPLOY_S3_BUCKET
113-
AWS_CODE_DEPLOY_S3_KEY_PREFIX
114-
AWS_CODE_DEPLOY_S3_FILENAME
115-
AWS_CODE_DEPLOY_S3_LIMIT_BUCKET_FILES
116-
AWS_CODE_DEPLOY_S3_SSE
117-
AWS_CODE_DEPLOY_REVISION_DESCRIPTION
118-
AWS_CODE_DEPLOY_DEPLOYMENT_DESCRIPTION
119-
```
120124

121125
## Examples
122126

@@ -291,7 +295,7 @@ This step ensures the deployment group exists within the specified application.
291295
Environment Variables:
292296

293297
* `AWS_CODE_DEPLOY_DEPLOYMENT_GROUP_NAME` (required): Deployment group name
294-
* `AWS_CODE_DEPLOY_SERVICE_ROLE_ARN` (required): Service role arn giving permissions to use Code Deploy when creating a deployment group
298+
* `AWS_CODE_DEPLOY_SERVICE_ROLE_ARN` (optional): Service role arn giving permissions to use Code Deploy when creating a deployment group
295299
* `AWS_CODE_DEPLOY_EC2_TAG_FILTERS` (optional): EC2 tags to filter on when creating a deployment group. Specify as a string with the following comma separated keys:
296300
* **Key** *string*
297301
* **Value** *string*
@@ -323,7 +327,7 @@ This step consists to push the application to S3.
323327
Environment Variables:
324328

325329
* `AWS_CODE_DEPLOY_S3_BUCKET` (required): The name of the S3 bucket to deploy the revision
326-
* `AWS_CODE_DEPLOY_S3_KEY_PREFIX` (optional): A prefix to use for the file key. It's highly recommended to structure a bucket with a prefix per deployment group. This allows to limit stored revisions per deployment group. Note: A leading or trailing slash is not required.
330+
* `AWS_CODE_DEPLOY_S3_KEY_PREFIX` (optional): A prefix to use for the file key. It's highly recommended to structure a bucket with a prefix per deployment group. This allows to limit stored revisions per deployment group. Note: A leading or trailing slash is not required.
327331

328332
For example:
329333

bin/aws-code-deploy.sh

+15-22
Original file line numberDiff line numberDiff line change
@@ -181,45 +181,38 @@ fi
181181

182182
h1 "Step 2: Configuring AWS"
183183
if [ -z "$AWS_CODE_DEPLOY_KEY" ]; then
184-
if [ ! -e ~/.aws/config ]; then
185-
error "Please configure AWS credentials or explicitly set the \"\$AWS_CODE_DEPLOY_KEY\" variable"
186-
exit 1
187-
fi
188-
if [ $(grep aws_access_key_id ~/.aws/config | wc -l) -lt 1 ]; then
189-
error "Unable to find \"aws_access_key_id\" in ~/.aws/config. Please configure AWS credentials or explicitly set the \"\$AWS_CODE_DEPLOY_KEY\" variable"
184+
# Ensure an access key has already been set
185+
if [ $(aws configure list | grep access_key | wc -l) -lt 1 ]; then
186+
error "No AWS_CODE_DEPLOY_KEY specified and AWS cli is not configured with an access key via env, config, or shared credentials"
190187
exit 1
191188
fi
192189
success "AWS Access Key already configured."
193190
else
194-
CONFIGURE_KEY_OUTPUT=$(aws configure set aws_access_key_id $AWS_CODE_DEPLOY_KEY 2>&1)
191+
$(aws configure set aws_access_key_id $AWS_CODE_DEPLOY_KEY 2>&1)
195192
success "Successfully configured AWS Access Key ID."
196193
fi
197194

198195
if [ -z "$AWS_CODE_DEPLOY_SECRET" ]; then
199-
if [ ! -e ~/.aws/config ]; then
200-
error "Please configure AWS credentials or explicitly set the \"\$AWS_CODE_DEPLOY_SECRET\" variable"
201-
exit 1
202-
fi
203-
if [ $(grep aws_secret_access_key ~/.aws/config | wc -l) -lt 1 ]; then
204-
error "Unable to find \"aws_secret_access_key\" in ~/.aws/config. Please configure AWS credentials or explicitly set the \"\$AWS_CODE_DEPLOY_SECRET\" variable"
196+
# Ensure an access key secret has already been set
197+
if [ $(aws configure list | grep secret_key | wc -l) -lt 1 ]; then
198+
error "No AWS_CODE_DEPLOY_SECRET specified and AWS cli is not configured with an access secret via env, config, or shared credentials"
205199
exit 1
206200
fi
207201
success "AWS Secret Access Key already configured."
208202
else
209-
CONFIGURE_KEY_OUTPUT=$(aws configure set aws_secret_access_key $AWS_CODE_DEPLOY_SECRET 2>&1)
203+
$(aws configure set aws_secret_access_key $AWS_CODE_DEPLOY_SECRET 2>&1)
210204
success "Successfully configured AWS Secret Access Key ID."
211205
fi
212206

213207
if [ -z "$AWS_CODE_DEPLOY_REGION" ]; then
214-
if [ -e ~/.aws/config ]; then
215-
if [ $(grep region ~/.aws/config | wc -l) -lt 1 ]; then
216-
warnNotice "Unable to configure AWS region."
217-
else
218-
success "AWS Region already configured."
219-
fi
208+
# Ensure AWS region has already been set
209+
if [ $(aws configure list | grep region | wc -l) -lt 1 ]; then
210+
error "No AWS_CODE_DEPLOY_REGION specified and AWS cli is not configured with an existing default region via env, config, or shared credentials"
211+
exit 1
220212
fi
213+
success "AWS Region already configured."
221214
else
222-
CONFIGURE_REGION_OUTPUT=$(aws configure set default.region $AWS_CODE_DEPLOY_REGION 2>&1)
215+
$(aws configure set default.region $AWS_CODE_DEPLOY_REGION 2>&1)
223216
success "Successfully configured AWS default region."
224217
fi
225218

@@ -610,4 +603,4 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
610603

611604
sleep 2
612605
done
613-
fi
606+
fi

0 commit comments

Comments
 (0)