Skip to content

Commit 3b85b40

Browse files
committed
Fixed examples (ECS related)
1 parent 8c46e20 commit 3b85b40

File tree

5 files changed

+56
-120
lines changed

5 files changed

+56
-120
lines changed

examples/complete/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Note that this example may create resources which cost money. Run `terraform des
3838
|------|--------|---------|
3939
| <a name="module_bucket"></a> [bucket](#module\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 5.0 |
4040
| <a name="module_disabled"></a> [disabled](#module\_disabled) | ../../ | n/a |
41-
| <a name="module_ecs"></a> [ecs](#module\_ecs) | terraform-aws-modules/ecs/aws | ~> 3.0 |
41+
| <a name="module_ecs"></a> [ecs](#module\_ecs) | terraform-aws-modules/ecs/aws | ~> 6.0 |
4242
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | n/a |
4343
| <a name="module_lambda"></a> [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | ~> 8.0 |
4444
| <a name="module_sns"></a> [sns](#module\_sns) | terraform-aws-modules/sns/aws | ~> 6.0 |
@@ -50,19 +50,16 @@ Note that this example may create resources which cost money. Run `terraform des
5050
|------|------|
5151
| [aws_cloudtrail.trail](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudtrail) | resource |
5252
| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
53-
| [aws_ecs_service.hello_world](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
54-
| [aws_ecs_task_definition.hello_world](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
5553
| [aws_kinesis_stream.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kinesis_stream) | resource |
5654
| [aws_sqs_queue.dlq](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
5755
| [aws_sqs_queue.fifo](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
5856
| [aws_sqs_queue.queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
5957
| [aws_sqs_queue_policy.queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_policy) | resource |
6058
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
6159
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
62-
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
63-
| [aws_iam_policy_document.bucket_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
6460
| [aws_iam_policy_document.queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
65-
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
61+
| [aws_subnets.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
62+
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
6663

6764
## Inputs
6865

examples/complete/main.tf

Lines changed: 46 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module "eventbridge" {
3838
append_rule_postfix = false
3939

4040
attach_ecs_policy = true
41-
ecs_target_arns = [aws_ecs_task_definition.hello_world.arn]
41+
ecs_target_arns = [module.ecs.services.hello-world.task_definition_arn]
4242

4343
rules = {
4444
orders = {
@@ -114,11 +114,11 @@ module "eventbridge" {
114114
},
115115
{
116116
name = "process-email-with-ecs-task",
117-
arn = module.ecs.ecs_cluster_arn,
117+
arn = module.ecs.cluster_arn,
118118
attach_role_arn = true
119119
ecs_target = {
120120
task_count = 1
121-
task_definition_arn = aws_ecs_task_definition.hello_world.arn
121+
task_definition_arn = module.ecs.services.hello-world.task_definition_arn
122122
}
123123
}
124124
]
@@ -229,6 +229,19 @@ locals {
229229
EOF
230230
}
231231
}
232+
#############################################################
233+
# Data sources to get VPC and default security group details
234+
#############################################################
235+
data "aws_vpc" "default" {
236+
default = true
237+
}
238+
239+
data "aws_subnets" "default" {
240+
filter {
241+
name = "vpc-id"
242+
values = [data.aws_vpc.default.id]
243+
}
244+
}
232245

233246
##################
234247
# Extra resources
@@ -314,37 +327,36 @@ module "step_function" {
314327

315328
module "ecs" {
316329
source = "terraform-aws-modules/ecs/aws"
317-
version = "~> 3.0"
318-
319-
name = random_pet.this.id
320-
321-
capacity_providers = ["FARGATE", "FARGATE_SPOT"]
322-
}
323-
324-
resource "aws_ecs_service" "hello_world" {
325-
name = "hello_world-${random_pet.this.id}"
326-
cluster = module.ecs.ecs_cluster_id
327-
task_definition = aws_ecs_task_definition.hello_world.arn
328-
329-
desired_count = 1
330+
version = "~> 6.0"
330331

331-
deployment_maximum_percent = 100
332-
deployment_minimum_healthy_percent = 0
333-
}
332+
cluster_name = random_pet.this.id
334333

335-
resource "aws_ecs_task_definition" "hello_world" {
336-
family = "hello_world-${random_pet.this.id}"
334+
default_capacity_provider_strategy = {
335+
FARGATE = {
336+
weight = 100
337+
base = 20
338+
}
339+
FARGATE_SPOT = {
340+
weight = 100
341+
}
342+
}
337343

338-
container_definitions = <<EOF
339-
[
340-
{
341-
"name": "hello_world-${random_pet.this.id}",
342-
"image": "hello-world",
343-
"cpu": 0,
344-
"memory": 128
344+
services = {
345+
hello-world = {
346+
subnet_ids = data.aws_subnets.default.ids
347+
desired_count = 1
348+
deployment_maximum_percent = 100
349+
deployment_minimum_healthy_percent = 0
350+
351+
container_definitions = {
352+
hello-world = {
353+
image = "hello-world",
354+
cpu = 0,
355+
memory = 128
356+
}
357+
}
358+
}
345359
}
346-
]
347-
EOF
348360
}
349361

350362
#############################################
@@ -357,7 +369,7 @@ module "lambda" {
357369

358370
function_name = "${random_pet.this.id}-lambda"
359371
handler = "index.lambda_handler"
360-
runtime = "python3.12"
372+
runtime = "python3.13"
361373

362374
create_package = false
363375
local_existing_package = local.downloaded
@@ -432,84 +444,13 @@ resource "aws_cloudtrail" "trail" {
432444
# s3
433445
#######
434446

435-
data "aws_region" "current" {}
436-
437-
data "aws_caller_identity" "current" {}
438-
439447
module "bucket" {
440448
source = "terraform-aws-modules/s3-bucket/aws"
441449
version = "~> 5.0"
442450

443-
bucket = "${random_pet.this.id}-bucket"
444-
attach_policy = true
445-
policy = data.aws_iam_policy_document.bucket_policy.json
446-
447-
force_destroy = true
448-
}
449-
450-
# https://docs.aws.amazon.com/awscloudtrail/latest/userguide/create-s3-bucket-policy-for-cloudtrail.html
451-
data "aws_iam_policy_document" "bucket_policy" {
452-
statement {
453-
sid = "AWSCloudTrailAclCheck"
454-
principals {
455-
identifiers = ["cloudtrail.amazonaws.com"]
456-
type = "Service"
457-
}
458-
actions = ["s3:GetBucketAcl"]
459-
resources = [
460-
"arn:aws:s3:::${random_pet.this.id}-bucket"
461-
]
462-
condition {
463-
test = "StringEquals"
464-
values = ["arn:aws:cloudtrail:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:trail/${random_pet.this.id}-trail"]
465-
variable = "aws:SourceArn"
466-
}
467-
}
451+
bucket = "${random_pet.this.id}-bucket"
468452

469-
statement {
470-
sid = "AWSCloudTrailWrite"
471-
principals {
472-
identifiers = ["cloudtrail.amazonaws.com"]
473-
type = "Service"
474-
}
475-
actions = ["s3:PutObject"]
476-
resources = [
477-
"arn:aws:s3:::${random_pet.this.id}-bucket/*"
478-
]
479-
condition {
480-
test = "StringEquals"
481-
values = ["bucket-owner-full-control"]
482-
variable = "s3:x-amz-acl"
483-
}
484-
condition {
485-
test = "StringEquals"
486-
values = ["arn:aws:cloudtrail:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:trail/${random_pet.this.id}-trail"]
487-
variable = "aws:SourceArn"
488-
}
489-
}
453+
attach_cloudtrail_log_delivery_policy = true
490454

455+
force_destroy = true
491456
}
492-
493-
#######
494-
## Lambda
495-
#######
496-
#module "lambda" {
497-
# source = "terraform-aws-modules/lambda/aws"
498-
# version = "~> 8.0"
499-
#
500-
# function_name = "dev-cron-job"
501-
# description = "Lambda Serverless Job"
502-
# handler = "index.handler"
503-
# runtime = "nodejs14.x"
504-
# timeout = 900
505-
#
506-
# source_path = "../with-lambda-shceduling/lambda"
507-
#}
508-
#
509-
#resource "aws_lambda_permission" "crons_invoke" {
510-
# statement_id = "AllowExecutionFromCloudWatch"
511-
# action = "lambda:InvokeFunction"
512-
# function_name = module.lambda.lambda_function_name
513-
# principal = "events.amazonaws.com"
514-
# source_arn = module.eventbridge.eventbridge_rule_arns.orders
515-
#}

examples/complete/outputs.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ output "eventbridge_permissions" {
3232
output "eventbridge_connections" {
3333
description = "The EventBridge Connections created and their attributes"
3434
value = module.eventbridge.eventbridge_connections
35+
sensitive = true
3536
}
3637

3738
output "eventbridge_api_destinations" {

examples/with-ecs-scheduling/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Note that this example may create resources which cost money. Run `terraform des
3434

3535
| Name | Source | Version |
3636
|------|--------|---------|
37-
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | terraform-aws-modules/ecs/aws | ~> 5.0 |
37+
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | terraform-aws-modules/ecs/aws | ~> 6.0 |
3838
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | n/a |
3939

4040
## Resources

examples/with-ecs-scheduling/main.tf

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,17 @@ module "eventbridge" {
106106

107107
module "ecs_cluster" {
108108
source = "terraform-aws-modules/ecs/aws"
109-
version = "~> 5.0"
109+
version = "~> 6.0"
110110

111111
cluster_name = random_pet.this.id
112112

113-
fargate_capacity_providers = {
113+
default_capacity_provider_strategy = {
114114
FARGATE = {
115-
default_capacity_provider_strategy = {
116-
weight = 100
117-
}
115+
weight = 100
116+
base = 20
118117
}
119118
FARGATE_SPOT = {
120-
default_capacity_provider_strategy = {
121-
weight = 100
122-
}
119+
weight = 100
123120
}
124121
}
125122

0 commit comments

Comments
 (0)