Skip to content

Commit e86e1b4

Browse files
committed
Initial commit
0 parents  commit e86e1b4

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.terraform

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Testing ECS Exec
2+
3+
Run `terraform apply` to create the sample infra, and then test running a command inside the container:
4+
5+
6+
```
7+
0% aws ecs describe-services --cluster main --services ecs-exec | grep task
8+
"taskDefinition": "arn:aws:ecs:eu-west-1:262355063596:task-definition/ecs-exec:6",
9+
"taskDefinition": "arn:aws:ecs:eu-west-1:262355063596:task-definition/ecs-exec:6",
10+
"message": "(service ecs-exec) has started 1 tasks: (task 501baa96db6e41c2be390bf7d631aeba)."
11+
0% aws ecs execute-command --cluster main \
12+
--task 501baa96db6e41c2be390bf7d631aeba \
13+
--container amazon-linux \
14+
--interactive \
15+
--command "/bin/sh"
16+
17+
The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.
18+
19+
20+
Starting session with SessionId: ecs-execute-command-044e1413fda145ea9
21+
sh-4.2# uname -a
22+
Linux ip-172-31-4-218.eu-west-1.compute.internal 4.14.252-195.483.amzn2.x86_64 #1 SMP Mon Nov 1 20:58:46 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
23+
sh-4.2#
24+
```

main.tf

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
resource "aws_ecs_cluster" "main" {
2+
name = "main"
3+
4+
setting {
5+
name = "containerInsights"
6+
value = "enabled"
7+
}
8+
}
9+
10+
resource "aws_iam_role" "ecs-exec" {
11+
name = "ecs-exec"
12+
assume_role_policy = jsonencode(
13+
{
14+
Version = "2012-10-17"
15+
Statement = [
16+
{
17+
Effect = "Allow"
18+
Principal = {
19+
Service = [
20+
"ecs-tasks.amazonaws.com"
21+
]
22+
}
23+
Action = "sts:AssumeRole"
24+
}
25+
]
26+
})
27+
28+
inline_policy {
29+
name = "ecs_exec"
30+
policy = jsonencode({
31+
Version = "2012-10-17"
32+
Statement = [
33+
{
34+
Effect = "Allow"
35+
Action = [
36+
"ssmmessages:CreateControlChannel",
37+
"ssmmessages:CreateDataChannel",
38+
"ssmmessages:OpenControlChannel",
39+
"ssmmessages:OpenDataChannel"
40+
],
41+
Resource = "*"
42+
}]})
43+
}
44+
}
45+
46+
resource "aws_ecs_task_definition" "ecs-exec" {
47+
family = "ecs-exec"
48+
task_role_arn = aws_iam_role.ecs-exec.arn
49+
network_mode = "awsvpc"
50+
requires_compatibilities = [
51+
"EC2",
52+
"FARGATE"
53+
]
54+
cpu = ".25 vcpu"
55+
memory = ".5 gb"
56+
container_definitions = jsonencode ([
57+
{
58+
name = "amazon-linux"
59+
image = "amazonlinux:latest"
60+
essential = true
61+
command = ["sleep","3600"]
62+
linuxParameters = {
63+
initProcessEnabled = true
64+
}
65+
}
66+
])
67+
}
68+
69+
resource "aws_ecs_service" "ecs-exec" {
70+
name = "ecs-exec"
71+
cluster = aws_ecs_cluster.main.arn
72+
73+
depends_on = [aws_iam_role.ecs-exec]
74+
75+
task_definition = aws_ecs_task_definition.ecs-exec.arn
76+
desired_count = 1
77+
launch_type = "FARGATE"
78+
enable_execute_command = true
79+
80+
network_configuration {
81+
subnets = data.aws_subnets.default.ids
82+
security_groups = [aws_security_group.ecs-exec.id]
83+
assign_public_ip = true
84+
}
85+
}
86+
87+
data "aws_vpc" "default" {
88+
default = true
89+
}
90+
91+
data "aws_subnets" "default" {
92+
filter {
93+
name = "vpc-id"
94+
values = [data.aws_vpc.default.id]
95+
}
96+
}
97+
98+
resource "aws_security_group" "ecs-exec" {
99+
name = "ecs-exec"
100+
vpc_id = data.aws_vpc.default.id
101+
102+
egress {
103+
description = "Outbound traffic"
104+
from_port = 0
105+
to_port = 0
106+
protocol = "-1"
107+
cidr_blocks = ["0.0.0.0/0"]
108+
}
109+
110+
}

setup.tf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 3.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
region = "eu-west-1"
12+
}

0 commit comments

Comments
 (0)