Skip to content

Commit 9b19e05

Browse files
authored
feat(deployment): enhanced region support (#162)
1 parent 8e45ff5 commit 9b19e05

File tree

14 files changed

+79
-15
lines changed

14 files changed

+79
-15
lines changed

data.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data "aws_caller_identity" "current" {}
2+
data "aws_partition" "current" {}
3+
data "aws_region" "current" {
4+
region = var.region
5+
}

examples/deployment/complete/codepipeline_step.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ locals {
33
}
44

55
resource "aws_cloudwatch_log_group" "custom_step" {
6+
region = local.region
7+
68
name = "/aws/codebuild/${local.codebuild_name}"
79
retention_in_days = 1
810
}
911

1012
resource "aws_codebuild_project" "custom_step" {
13+
region = local.region
14+
1115
name = local.codebuild_name
1216
service_role = aws_iam_role.custom_codepipeline_step.arn
1317

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data "aws_caller_identity" "current" {}
2+
data "aws_region" "current" {
3+
region = local.region
4+
}
5+

examples/deployment/complete/main.tf

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
data "aws_caller_identity" "current" {}
2-
data "aws_region" "current" {}
3-
41
module "fixtures" {
52
source = "../../fixtures"
63
}
74

85
locals {
96
environment = "production"
107
function_name = module.fixtures.output_function_name
8+
region = "eu-central-1"
119
s3_key = "${local.function_name}/package/lambda.zip"
1210
}
1311

1412
module "lambda" {
1513
source = "../../../"
1614

15+
region = local.region
16+
1717
architectures = ["arm64"]
1818
description = "Example usage for an AWS Lambda deployed from S3 using CodePipeline and CodeDeploy with hooks."
1919
function_name = local.function_name
@@ -27,6 +27,8 @@ module "lambda" {
2727
}
2828

2929
resource "aws_cloudwatch_metric_alarm" "error_rate" {
30+
region = local.region
31+
3032
alarm_description = "${module.lambda.function_name} has a high error rate"
3133
alarm_name = "${module.lambda.function_name}-error-rate"
3234
comparison_operator = "GreaterThanOrEqualToThreshold"
@@ -79,6 +81,8 @@ resource "aws_cloudwatch_metric_alarm" "error_rate" {
7981
# ---------------------------------------------------------------------------------------------------------------------
8082

8183
resource "aws_lambda_alias" "this" {
84+
region = local.region
85+
8286
function_name = module.lambda.function_name
8387
function_version = module.lambda.version
8488
name = local.environment
@@ -91,6 +95,8 @@ resource "aws_lambda_alias" "this" {
9195
module "deployment" {
9296
source = "../../../modules/deployment"
9397

98+
region = local.region
99+
94100
alias_name = aws_lambda_alias.this.name
95101
codedeploy_appspec_hooks_after_allow_traffic_arn = module.traffic_hook.arn
96102
codedeploy_appspec_hooks_before_allow_traffic_arn = module.traffic_hook.arn
@@ -110,9 +116,6 @@ module "deployment" {
110116
name = "FOO"
111117
default_value = "BAR"
112118
description = "test with all config values"
113-
},
114-
{
115-
name = "BAR"
116119
}
117120
]
118121

@@ -147,6 +150,8 @@ module "deployment" {
147150
}
148151

149152
resource "aws_codedeploy_deployment_config" "canary" {
153+
region = local.region
154+
150155
deployment_config_name = "custom-lambda-canary-deployment-config"
151156
compute_platform = "Lambda"
152157

@@ -168,12 +173,14 @@ resource "aws_codedeploy_deployment_config" "canary" {
168173
module "traffic_hook" {
169174
source = "../../../"
170175

176+
region = local.region
177+
171178
architectures = ["arm64"]
172179
description = "Lambda function executed by CodeDeploy before and/or after allow traffic to deployed version."
173180
filename = data.archive_file.traffic_hook.output_path
174181
function_name = "codedeploy-hook-example"
175182
handler = "hook.handler"
176-
runtime = "python3.12"
183+
runtime = "python3.13"
177184
source_code_hash = data.archive_file.traffic_hook.output_base64sha256
178185
}
179186

@@ -208,11 +215,15 @@ resource "aws_iam_role_policy_attachment" "traffic_hook" {
208215
#trivy:ignore:AVD-AWS-0088
209216
#trivy:ignore:AVD-AWS-0132
210217
resource "aws_s3_bucket" "source" {
218+
region = local.region
219+
211220
bucket = "ci-${data.aws_caller_identity.current.account_id}-${data.aws_region.current.region}"
212221
force_destroy = true
213222
}
214223

215224
resource "aws_s3_bucket_versioning" "source" {
225+
region = local.region
226+
216227
bucket = aws_s3_bucket.source.id
217228

218229
versioning_configuration {
@@ -222,11 +233,15 @@ resource "aws_s3_bucket_versioning" "source" {
222233

223234
// make sure to enable S3 bucket notifications to start continuous deployment pipeline
224235
resource "aws_s3_bucket_notification" "source" {
236+
region = local.region
237+
225238
bucket = aws_s3_bucket.source.id
226239
eventbridge = true
227240
}
228241

229242
resource "aws_s3_bucket_public_access_block" "source" {
243+
region = local.region
244+
230245
block_public_acls = true
231246
block_public_policy = true
232247
bucket = aws_s3_bucket.source.id
@@ -237,6 +252,8 @@ resource "aws_s3_bucket_public_access_block" "source" {
237252
// this resource is only used for the initial `terraform apply` - all further
238253
// deployments are running on CodePipeline
239254
resource "aws_s3_object" "initial" {
255+
region = local.region
256+
240257
bucket = aws_s3_bucket.source.bucket
241258
key = local.s3_key
242259
source = module.fixtures.output_path

main.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
data "aws_region" "current" {}
2-
data "aws_caller_identity" "current" {}
3-
data "aws_partition" "current" {}
4-
51
locals {
62
function_arn = "arn:${data.aws_partition.current.partition}:lambda:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:function:${var.function_name}"
73
handler = var.package_type != "Zip" ? null : var.handler

modules/deployment/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ No modules.
455455
| <a name="input_ecr_image_tag"></a> [ecr\_image\_tag](#input\_ecr\_image\_tag) | The container tag used for ECR/container based deployments. | `string` | `"latest"` | no |
456456
| <a name="input_ecr_repository_name"></a> [ecr\_repository\_name](#input\_ecr\_repository\_name) | Name of the ECR repository source used for ECR/container based deployments, required for `package_type=Image`. | `string` | `""` | no |
457457
| <a name="input_function_name"></a> [function\_name](#input\_function\_name) | The name of your Lambda Function to deploy. | `string` | n/a | yes |
458+
| <a name="input_region"></a> [region](#input\_region) | Alternative region used in all region-aware resources. If not set, the provider's region will be used. | `string` | `null` | no |
458459
| <a name="input_s3_bucket"></a> [s3\_bucket](#input\_s3\_bucket) | Name of the bucket used for S3 based deployments, required for `package_type=Zip`. Make sure to enable S3 bucket notifications for this bucket for continuous deployment of your Lambda function, see https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventBridge.html. | `string` | `""` | no |
459460
| <a name="input_s3_key"></a> [s3\_key](#input\_s3\_key) | Object key used for S3 based deployments, required for `package_type=Zip`. | `string` | `""` | no |
460461
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to all resources supporting tags. | `map(string)` | `{}` | no |

modules/deployment/codebuild.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
resource "aws_cloudwatch_log_group" "this" {
2+
region = var.region
23

34
name = "/aws/codebuild/${var.function_name}"
45
retention_in_days = var.codebuild_cloudwatch_logs_retention_in_days
56
tags = var.tags
67
}
78

89
resource "aws_codebuild_project" "this" {
10+
region = var.region
11+
912
name = var.function_name
1013
service_role = var.codebuild_role_arn == "" ? aws_iam_role.codebuild_role[0].arn : var.codebuild_role_arn
1114
tags = var.tags

modules/deployment/codedeploy.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
resource "aws_codedeploy_app" "this" {
2+
region = var.region
3+
24
name = var.function_name
35
compute_platform = "Lambda"
46
}
57

68
resource "aws_codedeploy_deployment_group" "this" {
9+
region = var.region
10+
711
app_name = var.function_name
812
deployment_config_name = var.deployment_config_name
913
deployment_group_name = var.alias_name

modules/deployment/data.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data "aws_caller_identity" "current" {}
2+
data "aws_partition" "current" {}
3+
data "aws_region" "current" {
4+
region = var.region
5+
}

modules/deployment/main.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
data "aws_caller_identity" "current" {}
2-
data "aws_region" "current" {}
3-
data "aws_partition" "current" {}
4-
51
locals {
62
artifact_store_bucket = var.codepipeline_artifact_store_bucket != "" ? var.codepipeline_artifact_store_bucket : aws_s3_bucket.pipeline[0].bucket
73
artifact_store_bucket_arn = "arn:${data.aws_partition.current.partition}:s3:::${local.artifact_store_bucket}"
@@ -25,6 +21,8 @@ locals {
2521
resource "aws_codepipeline" "this" {
2622
depends_on = [aws_iam_role.codepipeline_role]
2723

24+
region = var.region
25+
2826
name = local.pipeline_name
2927
pipeline_type = var.codepipeline_type
3028
role_arn = var.codepipeline_role_arn == "" ? aws_iam_role.codepipeline_role[0].arn : var.codepipeline_role_arn
@@ -172,6 +170,8 @@ resource "aws_codepipeline" "this" {
172170
resource "aws_s3_bucket" "pipeline" {
173171
count = var.codepipeline_artifact_store_bucket == "" ? 1 : 0
174172

173+
region = var.region
174+
175175
bucket = "${local.bucket_name_prefix}-pipeline-${data.aws_caller_identity.current.account_id}-${data.aws_region.current.region}"
176176
force_destroy = true
177177
tags = var.tags
@@ -182,6 +182,8 @@ resource "aws_s3_bucket" "pipeline" {
182182
resource "aws_s3_bucket_server_side_encryption_configuration" "pipeline" {
183183
count = var.codepipeline_artifact_store_bucket == "" ? 1 : 0
184184

185+
region = var.region
186+
185187
bucket = aws_s3_bucket.pipeline[count.index].bucket
186188

187189
rule {
@@ -194,6 +196,8 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "pipeline" {
194196
resource "aws_s3_bucket_public_access_block" "source" {
195197
count = var.codepipeline_artifact_store_bucket == "" ? 1 : 0
196198

199+
region = var.region
200+
197201
bucket = aws_s3_bucket.pipeline[count.index].id
198202
block_public_acls = true
199203
block_public_policy = true

0 commit comments

Comments
 (0)