Skip to content

Commit

Permalink
feat: Added deploy module to do complex deployments using AWS CodeDep…
Browse files Browse the repository at this point in the history
…loy (#17)
  • Loading branch information
antonbabenko authored Jun 10, 2020
1 parent dafada2 commit 83f50be
Show file tree
Hide file tree
Showing 10 changed files with 843 additions and 11 deletions.
38 changes: 27 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Not supported, yet:

This Terraform module is the part of [serverless.tf framework](https://github.com/antonbabenko/serverless.tf), which aims to simplify all operations when working with the serverless in Terraform:

1. Build and install dependencies - [read more](#build)
2. Create, store, and use deployment packages - [read more](#package)
3. Create and update AWS Lambda Function and Lambda Layer - [see usage](#usage)
4. Publish and create aliases for AWS Lambda Function - [see usage](#usage)
5. Do complex deployments (eg, rolling, canary, rollbacks) - [read more](#deployment)
1. Build and install dependencies - [read more](#build).
2. Create, store, and use deployment packages - [read more](#package).
3. Create, update, and publish AWS Lambda Function and Lambda Layer - [see usage](#usage).
4. Create static and dynamic aliases for AWS Lambda Function - [see usage](#usage), see [modules/alias](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/modules/alias).
5. Do complex deployments (eg, rolling, canary, rollbacks, triggers) - [read more](#deployment), see [modules/deploy](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/modules/deploy).


## Features
Expand Down Expand Up @@ -124,7 +124,19 @@ module "lambda_function_existing_package_local" {

### Lambda Function with existing package (prebuilt) stored in S3 bucket

Note that this module does not copy prebuilt packages into S3 bucket. This module can only store packages it builds locally and in S3 bucket.

```hcl
locals {
my_function_source = "../path/to/package.zip"
}
resource "aws_s3_bucket_object" "my_function" {
bucket = "my-bucket-with-lambda-builds"
key = "${filemd5(local.my_function_source)}.zip"
source = local.my_function_source
}
module "lambda_function_existing_package_s3" {
source = "terraform-aws-modules/lambda/aws"
Expand All @@ -136,7 +148,7 @@ module "lambda_function_existing_package_s3" {
create_package = false
s3_existing_package = {
bucket = "my-bucket-with-lambda-builds"
key = "existing_package.zip"
key = aws_s3_bucket_object.my_function.id
}
}
```
Expand Down Expand Up @@ -466,7 +478,9 @@ In simple terms, Lambda alias is like a pointer to either one version of Lambda

One Lambda Function can be used in multiple aliases. Using aliases gives large control of which version deployed when having multiple environments.

There is [alias module](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/modules/alias), which simplifies working with alias (create, manage configurations, updates, etc).
There is [alias module](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/modules/alias), which simplifies working with alias (create, manage configurations, updates, etc). See [examples/alias](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/alias) for various use-cases how aliases can be configured and used.

There is [deploy module](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/modules/deploy), which creates required resources to do deployments using AWS CodeDeploy. It also creates the deployment, and wait for completion. See [examples/deploy](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/deploy) for complete end-to-end build/update/deploy process.


## FAQ
Expand All @@ -488,10 +502,12 @@ A2: Delete an existing zip-archive from `builds` directory, or make a change in

## Examples

* [Complete](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/complete) - Create Lambda resources in various combinations with all supported features
* [Build and Package](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/build-package) - Build and create deployment packages in various ways
* [Async Invocations](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/async) - Create Lambda Function with async event configuration (with SQS and SNS integration)
* [With VPC](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/with-vpc) - Create Lambda Function with VPC
* [Complete](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/complete) - Create Lambda resources in various combinations with all supported features.
* [Build and Package](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/build-package) - Build and create deployment packages in various ways.
* [Alias](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/alias) - Create static and dynamic aliases in various ways.
* [Deploy](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/deploy) - Complete end-to-end build/update/deploy process using AWS CodeDeploy.
* [Async Invocations](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/async) - Create Lambda Function with async event configuration (with SQS and SNS integration).
* [With VPC](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/with-vpc) - Create Lambda Function with VPC.


<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Expand Down
2 changes: 2 additions & 0 deletions examples/deploy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
builds/*
deploy_script_*.txt
46 changes: 46 additions & 0 deletions examples/deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Lambda Function Deployments using AWS CodeDeploy

Configuration in this directory creates Lambda Function, Alias, and all resources required to create deployments using AWS CodeDeploy, and then it does a real deployment.

## Usage

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| aws | n/a |
| random | n/a |

## Inputs

No input.

## Outputs

| Name | Description |
|------|-------------|
| appspec | n/a |
| appspec\_content | n/a |
| appspec\_sha256 | n/a |
| codedeploy\_iam\_role\_name | Name of IAM role used by CodeDeploy |
| deploy\_script | n/a |
| script | n/a |
| this\_codedeploy\_app\_name | Name of CodeDeploy application |
| this\_codedeploy\_deployment\_group\_id | CodeDeploy deployment group name |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
91 changes: 91 additions & 0 deletions examples/deploy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
provider "aws" {
region = "eu-west-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
skip_requesting_account_id = true
}

resource "random_pet" "this" {
length = 2
}

module "lambda_function" {
source = "../../"

function_name = "${random_pet.this.id}-lambda"
handler = "index.lambda_handler"
runtime = "python3.8"
publish = true

source_path = "${path.module}/../fixtures/python3.8-app1"
hash_extra = "yo1"

allowed_triggers = {
APIGatewayAny = {
service = "apigateway"
arn = "arn:aws:execute-api:eu-west-1:135367859851:aqnku8akd0"
}
}
}

module "alias_refresh" {
source = "../../modules/alias"

refresh_alias = true

name = "current-with-refresh"

function_name = module.lambda_function.this_lambda_function_name

# Set function_version when creating alias to be able to deploy using it,
# because AWS CodeDeploy doesn't understand $LATEST as CurrentVersion.
function_version = module.lambda_function.this_lambda_function_version
}

module "deploy" {
source = "../../modules/deploy"

alias_name = module.alias_refresh.this_lambda_alias_name
function_name = module.lambda_function.this_lambda_function_name

target_version = module.lambda_function.this_lambda_function_version
description = "This is my awesome deploy!"

create_app = true
app_name = "my-awesome-app"

create_deployment_group = true
deployment_group_name = "something"

create_deployment = true
save_deploy_script = true
wait_deployment_completion = true
force_deploy = true

attach_triggers_policy = true
triggers = {
start = {
events = ["DeploymentStart"]
name = "DeploymentStart"
target_arn = aws_sns_topic.sns1.arn
}
success = {
events = ["DeploymentSuccess"]
name = "DeploymentSuccess"
target_arn = aws_sns_topic.sns2.arn
}
}

}

resource "aws_sns_topic" "sns1" {
name_prefix = random_pet.this.id
}

resource "aws_sns_topic" "sns2" {
name_prefix = random_pet.this.id
}
34 changes: 34 additions & 0 deletions examples/deploy/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
output "this_codedeploy_app_name" {
description = "Name of CodeDeploy application"
value = module.deploy.this_codedeploy_app_name
}

output "this_codedeploy_deployment_group_id" {
description = "CodeDeploy deployment group name"
value = module.deploy.this_codedeploy_deployment_group_id
}

output "codedeploy_iam_role_name" {
description = "Name of IAM role used by CodeDeploy"
value = module.deploy.codedeploy_iam_role_name
}

output "appspec" {
value = module.deploy.appspec
}

output "appspec_content" {
value = module.deploy.appspec_content
}

output "appspec_sha256" {
value = module.deploy.appspec_sha256
}

output "script" {
value = module.deploy.script
}

output "deploy_script" {
value = module.deploy.deploy_script
}
Loading

0 comments on commit 83f50be

Please sign in to comment.