Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1
Add the orbs
stanza below your version, invoking the orb:
1
2
orbs:
aws-sam-serverless: circleci/aws-sam-serverless@6.0.3
Use aws-sam-serverless
elements in your existing workflows and jobs.
Test your Lambda functions in a staging environment before deploying to production with the "deploy" job using OIDC authentication. Import the aws-cli orb and authenticate using the aws-cli/setup command with a valid role-arn for OIDC authentication.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
version: '2.1'
orbs:
aws-cli: circleci/aws-cli@4.0
sam: circleci/aws-sam-serverless@5.0
jobs:
test_my_api:
docker:
- image: cimg/node:lts
steps:
- run: echo "Run your API tests here"
workflows:
test_and_deploy:
jobs:
- sam/deploy:
auth:
- aws-cli/setup:
profile: OIDC-USER
role_arn: arn:aws:iam::123456789012:role/VALID_OIDC_SAM_ROLE
context: CircleCI_OIDC_Token
name: deploy-staging
profile: OIDC-USER
s3_bucket: my-s3-bucket
stack_name: staging-stack
template: ./path/to/template.yml
- test_my_api:
requires:
- deploy-staging
- sam/deploy:
auth:
- aws-cli/setup:
profile: OIDC-USER
role_arn: arn:aws:iam::123456789012:role/VALID_OIDC_SAM_ROLE
context: CircleCI_OIDC_Token
name: deploy-production
profile: OIDC-USER
requires:
- test_my_api
stack_name: production-stack
template: ./path/to/template.yml
Deploy a Lambda Docker container via SAM using static AWS keys for authentication. Import the aws-cli orb and authenticate using the aws-cli/setup command with static AWS keys stored as env_vars (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '2.1'
orbs:
aws-cli: circleci/aws-cli@4.0
sam: circleci/aws-sam-serverless@5.0
jobs:
test_my_api:
docker:
- image: cimg/node:lts
steps:
- run: echo "Run your API tests here"
workflows:
deploy_image:
jobs:
- sam/deploy:
auth:
- aws-cli/setup
context: aws-credentials-context
image_repositories: $IMG_URI
stack_name: staging-stack
template: ./path/to/template.yml
Install the SAM CLI to interact with the CLI directly. using static AWS keys for authentication. Import the aws-cli orb and authenticate using the aws-cli/setup command with static AWS keys stored as env_vars (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
version: '2.1'
orbs:
aws-cli: circleci/aws-cli@4.0
sam: circleci/aws-sam-serverless@5.0
jobs:
build_app:
executor: sam/default
steps:
- checkout
- aws-cli/setup
- sam/install
- run: sam build
workflows:
my-workflow:
jobs:
- build_app
Install the SAM CLI to interact with the CLI directly.
1
2
3
4
5
6
7
8
9
10
11
12
13
version: '2.1'
orbs:
aws-cli: circleci/aws-cli@4.0
sam: circleci/aws-sam-serverless@5.0
jobs:
build_and_package:
executor: sam/default
steps:
- checkout
- aws-cli/install
- sam/install
- sam/local_start_api
workflows: null
Deploy serverless applications.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
arguments | Additional arguments to pass to the deploy command. e.x. (--resolve-image-repos) | No | '' | string |
auth | The authentication method used to access your AWS account. Import the aws-cli orb in your config and
provide the aws-cli/setup command to authenticate with your preferred method. View examples for more information.
| Yes | - | steps |
base_dir | Resolves relative paths to the function's source code with respect to this folder. Use this if the AWS SAM template and your source code aren't in the same enclosing folder. By default, relative paths are resolved with respect to the template's location. | No | '' | string |
build_arguments | Additional arguments to pass to the build command. e.g. (--parallel) | No | '' | string |
build_container_vars | Environment variables to pass into the build container. Each instance of this option takes a key-value pair, where the key is the environment variable, and the value is the environment variable's value. Separate each key value pair with a comma, e.g build_container_vars: Function1.GITHUB_TOKEN=$TOKEN1, GLOBAL_ENV_VAR=$VAR2 NOTE: use_container parameter must be set to true in order to use build_container_vars. | No | '' | string |
build_dir | The path to a folder where the built artifacts are stored. | No | '' | string |
build_parameter_overrides | AWS CloudFormation parameter overrides encoded as key=value pairs for the sam build command. Use the same format as the AWS CLI. | No | '' | string |
capabilities | Comma separated list of capabilities for AWS Cloudformation to create stacks. The only valid values are CAPABILITY_IAM and CAPABILITY_NAMED_IAM. If you have IAM resources, you can specify either capability. If you have IAM resources with custom names, you must specify CAPABILITY_NAMED_IAM. | No | CAPABILITY_IAM | string |
debug | Turns on debug logging. | No | false | boolean |
image_repositories | Comma separated list of the ECR image repositories to push the Lambda container. If this option is set, the SAM package command will be run automatically. | No | '' | string |
machine_image | The CircleCI Linux Machine VM Image for this job. Find other available machine images here: https://circleci.com/docs/2.0/configuration-reference/#available-machine_images
| No | ubuntu-2204:current | string |
no_fail_on_empty_changeset | Specify if deploy command should return a zero exit code if there are no changes to be made to the stack. | No | true | boolean |
parameter_overrides | AWS CloudFormation parameter overrides encoded as key=value pairs. Use the same format as the AWS CLI. | No | '' | string |
pre_deploy | Run these steps prior to deploying the application, but after the checkout, install, build, and package steps. | No | [] | steps |
profile_name | Select a specific profile from your credential file to get AWS credentials. | No | default | string |
python_version | If set, this version of Python will be installed and set with pyenv globally. ex: "3.7.0" This is only for the local environment and will not have any effect if use_container is enabled. | No | '' | string |
region | Sets the AWS Region of the service (for example, us-east-1). | No | ${AWS_DEFAULT_REGION} | string |
resolve_s3 | Automatically create an Amazon S3 bucket to use for packaging and deploying for non-guided deployments. If you specify the --guided option, then the AWS SAM CLI ignores --resolve_s3. If you specify both the --s3_bucket and --resolve_s3 options, then an error occurs. | No | false | boolean |
s3_bucket | The name of the S3 bucket where this command uploads the artifacts that are referenced in your template. If this option is set, the SAM package command will be run automatically. | No | '' | string |
stack_name | The name of the AWS CloudFormation stack you're deploying to. If you specify an existing stack, the command updates the stack. If you specify a new stack, the command creates it. | Yes | - | string |
template | The AWS SAM template file | Yes | - | string |
use_container | If your functions depend on packages that have natively compiled dependencies, use this flag to build your function inside an AWS Lambda-like Docker container. | No | true | boolean |
validate | Validate your template file. This will run unless manually set to false. | No | true | boolean |
version | SAM CLI version to be installed. | No | latest | string |
Build your Lambda source. The sam build command iterates through the functions in your application, looks for a manifest file (such as requirements.txt) that contains the dependencies, and automatically creates deployment artifacts that you can deploy to Lambda using the sam package and sam deploy commands. You can also use sam build in combination with other commands like sam local invoke to test your application locally. Ensure CLI has been installed before utilizing.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
base_dir | Resolves relative paths to the function's source code with respect to this folder. Use this if the AWS SAM template and your source code aren't in the same enclosing folder. By default, relative paths are resolved with respect to the template's location. | No | '' | string |
build_arguments | Additional arguments to pass to the build command. e.g. (--parallel) | No | '' | string |
build_container_vars | Environment variables to pass into the build container. Each instance of this option takes a key-value pair, where the key is the environment variable, and the value is the environment variable's value. Separate each key value pair with a comma, e.g build_container_vars: Function1.GITHUB_TOKEN=$TOKEN1, GLOBAL_ENV_VAR=$VAR2 NOTE: use_container parameter must be set to true in order to use build_container_vars. | No | '' | string |
build_dir | The path to a folder where the built artifacts are stored. | No | '' | string |
build_parameter_overrides | AWS CloudFormation parameter overrides encoded as key=value pairs for the sam build command. Use the same format as the AWS CLI. | No | '' | string |
debug | Turns on debug logging. | No | false | boolean |
profile_name | Select a specific profile from your credential file to get AWS credentials. | No | default | string |
region | Sets the AWS Region of the service (for example, us-east-1). | No | ${AWS_DEFAULT_REGION} | string |
template | The AWS SAM template file | Yes | - | string |
use_container | If your functions depend on packages that have natively compiled dependencies, use this flag to build your function inside an AWS Lambda-like Docker container. | No | true | boolean |
validate | Validate your template file. This will run unless manually set to false. | No | true | boolean |
Deploys an AWS SAM application. It is recommended to create a Workflow to build and deploy your SAM app to a development stack for testing before deploying to a production stack.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
arguments | Additional arguments to pass to the deploy command. e.x. (--resolve-image-repos) | No | '' | string |
capabilities | Comma separated list of capabilities for AWS Cloudformation to create stacks. The only valid values are CAPABILITY_IAM and CAPABILITY_NAMED_IAM. If you have IAM resources, you can specify either capability. If you have IAM resources with custom names, you must specify CAPABILITY_NAMED_IAM. | No | CAPABILITY_IAM | string |
debug | Turns on debug logging. | No | false | boolean |
image_repositories | Comma separated list of the ECR image repositories to push the Lambda container. If this option is set, the SAM package command will be run automatically. | No | '' | string |
no_fail_on_empty_changeset | Specify if deploy command should return a zero exit code if there are no changes to be made to the stack. | No | true | boolean |
parameter_overrides | AWS CloudFormation parameter overrides encoded as key=value pairs. Use the same format as the AWS CLI. | No | '' | string |
profile_name | Select a specific profile from your credential file to get AWS credentials. | No | default | string |
region | (defaults to AWS_DEFAULT_REGION)
| No | ${AWS_DEFAULT_REGION} | string |
resolve_s3 | Automatically create an Amazon S3 bucket to use for packaging and deploying for non-guided deployments. If you specify the --guided option, then the AWS SAM CLI ignores --resolve-s3. If you specify both the --s3-bucket and --resolve-s3 options, then an error occurs. | No | false | boolean |
s3_bucket | The name of the S3 bucket where this command uploads the artifacts that are referenced in your template. If this option is set, the SAM package command will be run automatically. | No | '' | string |
stack_name | The name of the AWS CloudFormation stack you're deploying to. If you specify an existing stack, the command updates the stack. If you specify a new stack, the command creates it. | Yes | - | string |
template | The path where your AWS SAM template file is located. | No | '' | string |
Install the AWS SAM CLI.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
python_version | If set, this version of Python will be installed and set with pyenv globally. ex: "3.7.0" This is only for the local environment and will not have any effect if use_container is enabled. | No | '' | string |
version | SAM CLI version to be installed. | No | latest | string |
Run your SAM app locally in the background and wait until the server is online. This is an alias to the SAM CLI.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
debug | Enable debug logging
| No | false | boolean |
dockerize_version | Dockerize is a utility that will continuously attempt to connect to an endpoint. Used to wait for the API to come online. | No | 0.6.1 | string |
endpoint | After invoking the local-api this endpoint will be tested against until it is online before allowing the job to continue. | Yes | - | string |
env_vars | The JSON file that contains values for the Lambda function's environment variables. | No | '' | string |
port | The local port number to listen on | No | 3000 | integer |
template | The path where your AWS SAM template file is located. | No | '' | string |
timeout | The number in minutes to wait for the API to come online. | No | 5 | integer |
warm_containers | Specifies how AWS SAM CLI manages containers for each function with two available options: EAGER: Containers for all functions are loaded at startup and persist between invocations. LAZY: Containers are only loaded when each function is first invoked. Those containers persist for additional invocations. | No | '' | enum |
Machine executor provided by CircleCI. Needed for privileged access to Docker for the SAM CLI.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
machine_image | The CircleCI Linux Machine VM Image for this job. Find other available machine images here: https://circleci.com/docs/2.0/configuration-reference/#available-machine-images
| No | ubuntu-2204:current | string |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
Build, Test, and Deploy your AWS serverless applications on CircleCI utilizing the AWS Serverless Application Model.
display:
home_url: https://aws.amazon.com/serverless/sam/
source_url: https://github.com/CircleCI-Public/aws-sam-serverless-orb
commands:
build:
description: |
Build your Lambda source. The sam build command iterates through the functions in your application, looks for a manifest file (such as requirements.txt) that contains the dependencies, and automatically creates deployment artifacts that you can deploy to Lambda using the sam package and sam deploy commands. You can also use sam build in combination with other commands like sam local invoke to test your application locally.
Ensure CLI has been installed before utilizing.
parameters:
base_dir:
default: ""
description: Resolves relative paths to the function's source code with respect to this folder. Use this if the AWS SAM template and your source code aren't in the same enclosing folder. By default, relative paths are resolved with respect to the template's location.
type: string
build_arguments:
default: ""
description: Additional arguments to pass to the build command. e.g. (--parallel)
type: string
build_container_vars:
default: ""
description: 'Environment variables to pass into the build container. Each instance of this option takes a key-value pair, where the key is the environment variable, and the value is the environment variable''s value. Separate each key value pair with a comma, e.g build_container_vars: Function1.GITHUB_TOKEN=$TOKEN1, GLOBAL_ENV_VAR=$VAR2 NOTE: use_container parameter must be set to true in order to use build_container_vars.'
type: string
build_dir:
default: ""
description: The path to a folder where the built artifacts are stored.
type: string
build_parameter_overrides:
default: ""
description: AWS CloudFormation parameter overrides encoded as key=value pairs for the sam build command. Use the same format as the AWS CLI.
type: string
debug:
default: false
description: Turns on debug logging.
type: boolean
profile_name:
default: default
description: Select a specific profile from your credential file to get AWS credentials.
type: string
region:
default: ${AWS_DEFAULT_REGION}
description: Sets the AWS Region of the service (for example, us-east-1).
type: string
template:
description: The AWS SAM template file
type: string
use_container:
default: true
description: If your functions depend on packages that have natively compiled dependencies, use this flag to build your function inside an AWS Lambda-like Docker container.
type: boolean
validate:
default: true
description: Validate your template file. This will run unless manually set to false.
type: boolean
steps:
- when:
condition: << parameters.validate >>
steps:
- run:
command: |
sam validate -t << parameters.template >> \
--region << parameters.region >> \
--profile << parameters.profile_name >> \
<<# parameters.debug >>--debug<</ parameters.debug >>
name: Validating SAM template
- run:
command: |
#!/bin/bash
set -o noglob
ORB_STR_REGION="$(echo "${ORB_STR_REGION}" | circleci env subst)"
ORB_STR_TEMPLATE="$(echo "${ORB_STR_TEMPLATE}" | circleci env subst)"
ORB_STR_BUILD_PARAMETER_OVERRIDES="$(echo "${ORB_STR_BUILD_PARAMETER_OVERRIDES}" | circleci env subst)"
ORB_STR_BUILD_ARGUMENTS="$(echo "${ORB_STR_BUILD_ARGUMENTS}" | circleci env subst)"
ORB_STR_BUILD_CONTAINER_VARS="$(echo "${ORB_STR_BUILD_CONTAINER_VARS}" | circleci env subst)"
ORB_EVAL_BUILD_DIR="$(eval echo "${ORB_EVAL_BUILD_DIR}" | circleci env subst)"
ORB_EVAL_BASE_DIR="$(eval echo "${ORB_EVAL_BASE_DIR}" | circleci env subst)"
set -- "$@" --template-file "${ORB_STR_TEMPLATE}"
set -- "$@" --region "${ORB_STR_REGION}"
if [ -n "$ORB_EVAL_BUILD_DIR" ]; then
set -- "$@" --build-dir "${ORB_EVAL_BUILD_DIR}"
fi
if [ -n "$ORB_EVAL_BASE_DIR" ]; then
set -- "$@" --base-dir "${ORB_EVAL_BASE_DIR}"
fi
if [ "$ORB_BOOL_USE_CONTAINER" -eq 0 ] && [ -n "$ORB_STR_BUILD_CONTAINER_VARS" ]; then
echo "Error - use_container parameter set to false. Set to true to use build_container_vars"
exit 1;
elif [ "$ORB_BOOL_USE_CONTAINER" -eq 1 ]; then
set -- "$@" --use-container
if [ -n "$ORB_STR_BUILD_CONTAINER_VARS" ]; then
IFS="," read -ra container_vars \<<< "${ORB_STR_BUILD_CONTAINER_VARS}"
for var in "${container_vars[@]}"; do
set -- "$@" --container-env-var "${var}"
done
fi
fi
if [ "$ORB_BOOL_DEBUG" -eq 1 ]; then
set -- "$@" --debug
fi
if [ -n "$ORB_STR_BUILD_PARAMETER_OVERRIDES" ]; then
set -- "$@" --parameter-overrides "${ORB_STR_BUILD_PARAMETER_OVERRIDES}"
fi
if [ -n "$ORB_STR_BUILD_ARGUMENTS" ]; then
# shellcheck disable=SC2086
set -- "$@" ${ORB_STR_BUILD_ARGUMENTS}
fi
set -x
sam build --profile "${ORB_STR_PROFILE_NAME}" "$@"
set +x
environment:
ORB_BOOL_DEBUG: << parameters.debug >>
ORB_BOOL_USE_CONTAINER: << parameters.use_container >>
ORB_EVAL_BASE_DIR: << parameters.base_dir >>
ORB_EVAL_BUILD_DIR: << parameters.build_dir >>
ORB_STR_BUILD_ARGUMENTS: << parameters.build_arguments >>
ORB_STR_BUILD_CONTAINER_VARS: << parameters.build_container_vars >>
ORB_STR_BUILD_PARAMETER_OVERRIDES: << parameters.build_parameter_overrides >>
ORB_STR_PROFILE_NAME: << parameters.profile_name >>
ORB_STR_REGION: << parameters.region >>
ORB_STR_TEMPLATE: << parameters.template >>
name: Building SAM application
deploy:
description: |
Deploys an AWS SAM application. It is recommended to create a Workflow to build and deploy your SAM app to a development stack for testing before deploying to a production stack.
parameters:
arguments:
default: ""
description: Additional arguments to pass to the deploy command. e.x. (--resolve-image-repos)
type: string
capabilities:
default: CAPABILITY_IAM
description: Comma separated list of capabilities for AWS Cloudformation to create stacks. The only valid values are CAPABILITY_IAM and CAPABILITY_NAMED_IAM. If you have IAM resources, you can specify either capability. If you have IAM resources with custom names, you must specify CAPABILITY_NAMED_IAM.
type: string
debug:
default: false
description: Turns on debug logging.
type: boolean
image_repositories:
default: ""
description: Comma separated list of the ECR image repositories to push the Lambda container. If this option is set, the SAM package command will be run automatically.
type: string
no_fail_on_empty_changeset:
default: true
description: Specify if deploy command should return a zero exit code if there are no changes to be made to the stack.
type: boolean
parameter_overrides:
default: ""
description: AWS CloudFormation parameter overrides encoded as key=value pairs. Use the same format as the AWS CLI.
type: string
profile_name:
default: default
description: Select a specific profile from your credential file to get AWS credentials.
type: string
region:
default: ${AWS_DEFAULT_REGION}
description: |
(defaults to AWS_DEFAULT_REGION)
type: string
resolve_s3:
default: false
description: Automatically create an Amazon S3 bucket to use for packaging and deploying for non-guided deployments. If you specify the --guided option, then the AWS SAM CLI ignores --resolve-s3. If you specify both the --s3-bucket and --resolve-s3 options, then an error occurs.
type: boolean
s3_bucket:
default: ""
description: The name of the S3 bucket where this command uploads the artifacts that are referenced in your template. If this option is set, the SAM package command will be run automatically.
type: string
stack_name:
description: The name of the AWS CloudFormation stack you're deploying to. If you specify an existing stack, the command updates the stack. If you specify a new stack, the command creates it.
type: string
template:
default: ""
description: The path where your AWS SAM template file is located.
type: string
steps:
- run:
command: |
#!/bin/bash
set -o noglob
ORB_STR_CAPABILITIES="$(echo "${ORB_STR_CAPABILITIES}" | circleci env subst)"
ORB_STR_IMAGE_REPO="$(echo "${ORB_STR_IMAGE_REPO}" | circleci env subst)"
ORB_STR_S3_BUCKET="$(echo "${ORB_STR_S3_BUCKET}" | circleci env subst)"
ORB_STR_TEMPLATE="$(echo "${ORB_STR_TEMPLATE}" | circleci env subst)"
ORB_STR_OVERRIDES="$(echo "${ORB_STR_OVERRIDES}" | circleci env subst)"
ORB_STR_ARGUMENTS="$(echo "${ORB_STR_ARGUMENTS}" | circleci env subst)"
ORB_STR_REGION="$(echo "${ORB_STR_REGION}" | circleci env subst)"
ORB_STR_STACK_NAME="$(echo "${ORB_STR_STACK_NAME}" | circleci env subst)"
ORB_STR_S3_BUCKET="$(echo "${ORB_STR_S3_BUCKET}" | circleci env subst)"
ORB_STR_OVERRIDES="$(echo "${ORB_STR_OVERRIDES}" | circleci env subst)"
IFS=', ' read -r -a ARRAY_CAPABILITIES \<<< "$ORB_STR_CAPABILITIES"
echo "${ARRAY_CAPABILITIES[@]}"
set -- "$@" --capabilities "${ARRAY_CAPABILITIES[@]}"
set -- "$@" --stack-name "${ORB_STR_STACK_NAME}"
set -- "$@" --region "${ORB_STR_REGION}"
if [ -n "$ORB_STR_IMAGE_REPO" ]; then
echo "DEBUG: set_image_repos called" "${ORB_STR_IMAGE_REPO}"| tee -a /tmp/sam.log
IFS=', ' read -r -a ARRAY_REPOSITORIES \<<< "${ORB_STR_IMAGE_REPO}"
REPOARRYLEN=${#ARRAY_REPOSITORIES[@]}
if [ "$REPOARRYLEN" = 1 ]; then
echo "DEBUG: Single image repo named ${ARRAY_REPOSITORIES[0]}" | tee -a /tmp/sam.log
set -- "$@" --image-repository "${ARRAY_REPOSITORIES[0]}"
else
for image in "${!ARRAY_REPOSITORIES[@]}"; do
echo "DEBUG: Multi image repo" "${ARRAY_REPOSITORIES[$image]}" | tee -a /tmp/sam.log
echo "Images: ${#ARRAY_REPOSITORIES[@]}" | tee -a /tmp/sam.log
echo "DEBUG: ${ARRAY_REPOSITORIES[image]}" | tee -a /tmp/sam.log
set -- "$@" --image-repositories "${!ARRAY_REPOSITORIES[0]}"
done
fi
fi
if [ -n "$ORB_STR_S3_BUCKET" ]; then
# Technically this shouldnt be needed as this shouldnt be possible given the order of execution
if [ -n "$ORB_STR_IMAGE_REPO" ]; then
echo " parameters.image-repository cannot be set if parameters.s3-bucket is also configured. Remove one of these options."
fi
set -- "$@" --s3-bucket "${ORB_STR_S3_BUCKET}"
fi
if [ -n "$ORB_STR_TEMPLATE" ]; then
set -- "$@" --template-file "${ORB_STR_TEMPLATE}"
fi
if [ "$ORB_BOOL_DEBUG" -eq 1 ]; then
set -- "$@" --debug
fi
if [ "$ORB_BOOL_NOFAIL" -eq 1 ]; then
set -- "$@" --no-fail-on-empty-changeset
fi
if [ "$ORB_BOOL_RESOLVE_S3" -eq 1 ]; then
set -- "$@" --resolve-s3
fi
if [ -n "$ORB_STR_OVERRIDES" ]; then
set -- "$@" --parameter-overrides "${ORB_STR_OVERRIDES}"
fi
if [ -n "$ORB_STR_ARGUMENTS" ]; then
# shellcheck disable=SC2086
set -- "$@" $ORB_STR_ARGUMENTS
fi
set -x
sam deploy --profile "${ORB_STR_PROFILE_NAME}" "$@"
set +x
environment:
ORB_BOOL_DEBUG: <<parameters.debug>>
ORB_BOOL_NOFAIL: <<parameters.no_fail_on_empty_changeset>>
ORB_BOOL_RESOLVE_S3: << parameters.resolve_s3 >>
ORB_STR_ARGUMENTS: << parameters.arguments >>
ORB_STR_CAPABILITIES: <<parameters.capabilities>>
ORB_STR_IMAGE_REPO: << parameters.image_repositories >>
ORB_STR_OVERRIDES: <<parameters.parameter_overrides>>
ORB_STR_PROFILE_NAME: <<parameters.profile_name>>
ORB_STR_REGION: <<parameters.region>>
ORB_STR_S3_BUCKET: << parameters.s3_bucket >>
ORB_STR_STACK_NAME: <<parameters.stack_name>>
ORB_STR_TEMPLATE: <<parameters.template>>
name: Deploy SAM application
install:
description: |
Install the AWS SAM CLI.
parameters:
python_version:
default: ""
description: 'If set, this version of Python will be installed and set with pyenv globally. ex: "3.7.0" This is only for the local environment and will not have any effect if use_container is enabled.'
type: string
version:
default: latest
description: SAM CLI version to be installed.
type: string
steps:
- when:
condition: << parameters.python_version >>
steps:
- run:
command: |
git clone git://github.com/pyenv/pyenv-update.git $(pyenv root)/plugins/pyenv-update
pyenv update
pyenv install << parameters.python_version >>
name: Installing Python << parameters.python_version >>
- run:
command: |
pyenv versions
pyenv global << parameters.python_version >>
name: Switching to Python << parameters.python_version >>
- run:
command: |
#!/bin/bash
cd /tmp || true
if [[ $EUID == 0 ]]; then export SUDO=""; else # Check if we are root
export SUDO="sudo"
fi
# Check if the AWS CLI is installed
if ! command -v aws &>/dev/null; then
echo "AWS CLI could not be found. Please install it before initiating the SAM CLI."
echo
echo "Use the circleci/aws-cli orb to install the AWS CLI."
echo "https://circleci.com/developer/orbs/orb/circleci/aws-sam-serverless#usage-examples"
echo
echo "Job example:"
echo "
jobs:
build_app:
executor: sam/default
steps:
- checkout
- aws-cli/setup
- sam/install
"
echo
echo "Workflow example:"
echo "
workflows:
test_and_deploy:
jobs:
- sam/deploy:
pre-steps:
- aws-cli/setup
"
exit 1
fi
if [[ $ORB_STR_VERSION == "latest" ]]; then
echo "Installing latest version of SAM CLI"
curl -L "https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip" -o aws-sam-cli-linux-x86_64.zip
else
echo "Installing SAM CLI version $ORB_STR_VERSION"
curl -L "https://github.com/aws/aws-sam-cli/releases/download/v${ORB_STR_VERSION}/aws-sam-cli-linux-x86_64.zip" -o aws-sam-cli-linux-x86_64.zip
fi
unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
$SUDO ./sam-installation/install
which sam
sam --version
environment:
ORB_STR_VERSION: << parameters.version >>
name: Install SAM CLI
local_start_api:
description: |
Run your SAM app locally in the background and wait until the server is online. This is an alias to the SAM CLI.
parameters:
debug:
default: false
description: |
Enable debug logging
type: boolean
dockerize_version:
default: 0.6.1
description: Dockerize is a utility that will continuously attempt to connect to an endpoint. Used to wait for the API to come online.
type: string
endpoint:
description: After invoking the local-api this endpoint will be tested against until it is online before allowing the job to continue.
type: string
env_vars:
default: ""
description: The JSON file that contains values for the Lambda function's environment variables.
type: string
port:
default: 3000
description: The local port number to listen on
type: integer
template:
default: ""
description: The path where your AWS SAM template file is located.
type: string
timeout:
default: 5
description: The number in minutes to wait for the API to come online.
type: integer
warm_containers:
default: ""
description: 'Specifies how AWS SAM CLI manages containers for each function with two available options: EAGER: Containers for all functions are loaded at startup and persist between invocations. LAZY: Containers are only loaded when each function is first invoked. Those containers persist for additional invocations.'
enum:
- ""
- EAGER
- LAZY
type: enum
steps:
- run:
command: |-
#!/bin/bash
ORB_STR_DOCKERIZE_VERSION="$(echo "${ORB_STR_DOCKERIZE_VERSION}" | circleci env subst)"
wget "https://github.com/jwilder/dockerize/releases/download/v${ORB_STR_DOCKERIZE_VERSION}/dockerize-linux-amd64-v${ORB_STR_DOCKERIZE_VERSION}.tar.gz" && \
sudo tar -C /usr/local/bin -xzvf "dockerize-linux-amd64-v${ORB_STR_DOCKERIZE_VERSION}.tar.gz" && \
rm "dockerize-linux-amd64-v${ORB_STR_DOCKERIZE_VERSION}.tar.gz"
environment:
ORB_STR_DOCKERIZE_VERSION: <<parameters.dockerize_version>>
name: install dockerize
- run:
background: true
command: |-
#!/bin/bash
ORB_STR_TEMPLATE="$(echo "${ORB_STR_TEMPLATE}" | circleci env subst)"
ORB_STR_ENV_VARS="$(echo "${ORB_STR_ENV_VARS}" | circleci env subst)"
if [ -n "$ORB_STR_TEMPLATE" ]; then
set -- "$@" -t "$ORB_STR_TEMPLATE"
fi
set -- "$@" -p "$ORB_INT_PORT"
if [ -n "$ORB_STR_ENV_VARS" ]; then
set -- "$@" -n "$PARAM_ENV_VARS"
fi
if [ -n "$ORB_ENUM_WARM_CONTAINERS" ]; then
set -- "$@" --warm-containers "$ORB_ENUM_WARM_CONTAINERS"
fi
if [ "$ORB_BOOL_DEBUG" -eq 1 ]; then
set -- "$@" --debug
fi
set -x
sam local start-api "$@"
set +x
environment:
ORB_BOOL_DEBUG: <<parameters.debug>>
ORB_ENUM_WARM_CONTAINERS: << parameters.warm_containers >>
ORB_INT_PORT: <<parameters.port>>
ORB_STR_ENV_VARS: <<parameters.env_vars>>
ORB_STR_TEMPLATE: <<parameters.template>>
name: SAM local start-api
- run:
command: |-
#!/bin/bash
ORB_STR_ENDPOINT="$(echo "${ORB_STR_ENDPOINT}" | circleci env subst)"
dockerize -wait "http://127.0.0.1:3000/${ORB_STR_ENDPOINT}" -timeout "${ORB_INT_TIMEOUT}m"
environment:
ORB_INT_TIMEOUT: <<parameters.timeout>>
ORB_STR_ENDPOINT: <<parameters.endpoint>>
name: Wait for API
executors:
default:
description: |
Machine executor provided by CircleCI. Needed for privileged access to Docker for the SAM CLI.
machine:
image: <<parameters.machine_image>>
parameters:
machine_image:
default: ubuntu-2204:current
description: |
The CircleCI Linux Machine VM Image for this job. Find other available machine images here: https://circleci.com/docs/2.0/configuration-reference/#available-machine-images
type: string
jobs:
deploy:
description: |
Deploy serverless applications.
executor:
machine_image: <<parameters.machine_image>>
name: default
parameters:
arguments:
default: ""
description: Additional arguments to pass to the deploy command. e.x. (--resolve-image-repos)
type: string
auth:
description: |
The authentication method used to access your AWS account. Import the aws-cli orb in your config and
provide the aws-cli/setup command to authenticate with your preferred method. View examples for more information.
type: steps
base_dir:
default: ""
description: Resolves relative paths to the function's source code with respect to this folder. Use this if the AWS SAM template and your source code aren't in the same enclosing folder. By default, relative paths are resolved with respect to the template's location.
type: string
build_arguments:
default: ""
description: Additional arguments to pass to the build command. e.g. (--parallel)
type: string
build_container_vars:
default: ""
description: 'Environment variables to pass into the build container. Each instance of this option takes a key-value pair, where the key is the environment variable, and the value is the environment variable''s value. Separate each key value pair with a comma, e.g build_container_vars: Function1.GITHUB_TOKEN=$TOKEN1, GLOBAL_ENV_VAR=$VAR2 NOTE: use_container parameter must be set to true in order to use build_container_vars.'
type: string
build_dir:
default: ""
description: The path to a folder where the built artifacts are stored.
type: string
build_parameter_overrides:
default: ""
description: AWS CloudFormation parameter overrides encoded as key=value pairs for the sam build command. Use the same format as the AWS CLI.
type: string
capabilities:
default: CAPABILITY_IAM
description: Comma separated list of capabilities for AWS Cloudformation to create stacks. The only valid values are CAPABILITY_IAM and CAPABILITY_NAMED_IAM. If you have IAM resources, you can specify either capability. If you have IAM resources with custom names, you must specify CAPABILITY_NAMED_IAM.
type: string
debug:
default: false
description: Turns on debug logging.
type: boolean
image_repositories:
default: ""
description: Comma separated list of the ECR image repositories to push the Lambda container. If this option is set, the SAM package command will be run automatically.
type: string
machine_image:
default: ubuntu-2204:current
description: |
The CircleCI Linux Machine VM Image for this job. Find other available machine images here: https://circleci.com/docs/2.0/configuration-reference/#available-machine_images
type: string
no_fail_on_empty_changeset:
default: true
description: Specify if deploy command should return a zero exit code if there are no changes to be made to the stack.
type: boolean
parameter_overrides:
default: ""
description: AWS CloudFormation parameter overrides encoded as key=value pairs. Use the same format as the AWS CLI.
type: string
pre_deploy:
default: []
description: Run these steps prior to deploying the application, but after the checkout, install, build, and package steps.
type: steps
profile_name:
default: default
description: Select a specific profile from your credential file to get AWS credentials.
type: string
python_version:
default: ""
description: 'If set, this version of Python will be installed and set with pyenv globally. ex: "3.7.0" This is only for the local environment and will not have any effect if use_container is enabled.'
type: string
region:
default: ${AWS_DEFAULT_REGION}
description: Sets the AWS Region of the service (for example, us-east-1).
type: string
resolve_s3:
default: false
description: Automatically create an Amazon S3 bucket to use for packaging and deploying for non-guided deployments. If you specify the --guided option, then the AWS SAM CLI ignores --resolve_s3. If you specify both the --s3_bucket and --resolve_s3 options, then an error occurs.
type: boolean
s3_bucket:
default: ""
description: The name of the S3 bucket where this command uploads the artifacts that are referenced in your template. If this option is set, the SAM package command will be run automatically.
type: string
stack_name:
description: The name of the AWS CloudFormation stack you're deploying to. If you specify an existing stack, the command updates the stack. If you specify a new stack, the command creates it.
type: string
template:
description: The AWS SAM template file
type: string
use_container:
default: true
description: If your functions depend on packages that have natively compiled dependencies, use this flag to build your function inside an AWS Lambda-like Docker container.
type: boolean
validate:
default: true
description: Validate your template file. This will run unless manually set to false.
type: boolean
version:
default: latest
description: SAM CLI version to be installed.
type: string
steps:
- checkout
- install:
python_version: << parameters.python_version >>
version: << parameters.version >>
- steps: << parameters.auth >>
- build:
base_dir: << parameters.base_dir >>
build_arguments: << parameters.build_arguments >>
build_container_vars: << parameters.build_container_vars >>
build_dir: << parameters.build_dir >>
build_parameter_overrides: << parameters.build_parameter_overrides >>
debug: << parameters.debug >>
profile_name: << parameters.profile_name >>
region: << parameters.region >>
template: << parameters.template >>
use_container: << parameters.use_container >>
validate: << parameters.validate >>
- steps: << parameters.pre_deploy >>
- deploy:
arguments: << parameters.arguments >>
capabilities: << parameters.capabilities >>
debug: << parameters.debug >>
image_repositories: << parameters.image_repositories >>
no_fail_on_empty_changeset: << parameters.no_fail_on_empty_changeset >>
parameter_overrides: << parameters.parameter_overrides >>
profile_name: << parameters.profile_name >>
region: << parameters.region >>
resolve_s3: << parameters.resolve_s3 >>
s3_bucket: << parameters.s3_bucket >>
stack_name: << parameters.stack_name >>
examples:
build_test_deploy_oidc:
description: |
Test your Lambda functions in a staging environment before deploying to production with the "deploy" job using OIDC authentication. Import the aws-cli orb and authenticate using the aws-cli/setup command with a valid role-arn for OIDC authentication.
usage:
version: "2.1"
orbs:
aws-cli: circleci/aws-cli@4.0
sam: circleci/aws-sam-serverless@5.0
jobs:
test_my_api:
docker:
- image: cimg/node:lts
steps:
- run: echo "Run your API tests here"
workflows:
test_and_deploy:
jobs:
- sam/deploy:
auth:
- aws-cli/setup:
profile: OIDC-USER
role_arn: arn:aws:iam::123456789012:role/VALID_OIDC_SAM_ROLE
context: CircleCI_OIDC_Token
name: deploy-staging
profile: OIDC-USER
s3_bucket: my-s3-bucket
stack_name: staging-stack
template: ./path/to/template.yml
- test_my_api:
requires:
- deploy-staging
- sam/deploy:
auth:
- aws-cli/setup:
profile: OIDC-USER
role_arn: arn:aws:iam::123456789012:role/VALID_OIDC_SAM_ROLE
context: CircleCI_OIDC_Token
name: deploy-production
profile: OIDC-USER
requires:
- test_my_api
stack_name: production-stack
template: ./path/to/template.yml
deploy_lambda_container:
description: |
Deploy a Lambda Docker container via SAM using static AWS keys for authentication. Import the aws-cli orb and authenticate using the aws-cli/setup command with static AWS keys stored as env_vars (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).
usage:
version: "2.1"
orbs:
aws-cli: circleci/aws-cli@4.0
sam: circleci/aws-sam-serverless@5.0
jobs:
test_my_api:
docker:
- image: cimg/node:lts
steps:
- run: echo "Run your API tests here"
workflows:
deploy_image:
jobs:
- sam/deploy:
auth:
- aws-cli/setup
context: aws-credentials-context
image_repositories: $IMG_URI
stack_name: staging-stack
template: ./path/to/template.yml
install_cli:
description: |
Install the SAM CLI to interact with the CLI directly. using static AWS keys for authentication. Import the aws-cli orb and authenticate using the aws-cli/setup command with static AWS keys stored as env_vars (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).
usage:
version: "2.1"
orbs:
aws-cli: circleci/aws-cli@4.0
sam: circleci/aws-sam-serverless@5.0
jobs:
build_app:
executor: sam/default
steps:
- checkout
- aws-cli/setup
- sam/install
- run: sam build
workflows:
my-workflow:
jobs:
- build_app
local_test:
description: |
Install the SAM CLI to interact with the CLI directly.
usage:
version: "2.1"
orbs:
aws-cli: circleci/aws-cli@4.0
sam: circleci/aws-sam-serverless@5.0
jobs:
build_and_package:
executor: sam/default
steps:
- checkout
- aws-cli/install
- sam/install
- sam/local_start_api
workflows: null