forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__main__.py
109 lines (98 loc) · 2.52 KB
/
__main__.py
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
from pulumi import export, ResourceOptions
import pulumi_aws as aws
import json
# Create an ECS cluster to run a container-based service.
cluster = aws.ecs.Cluster('cluster')
# Read back the default VPC and public subnets, which we will use.
default_vpc = aws.ec2.get_vpc(default='true')
default_vpc_subnets = aws.ec2.get_subnet_ids(vpc_id=default_vpc.id)
# Create a SecurityGroup that permits HTTP ingress and unrestricted egress.
group = aws.ec2.SecurityGroup('web-secgrp',
vpc_id=default_vpc.id,
description='Enable HTTP access',
ingress=[{
'protocol': 'tcp',
'from_port': 80,
'to_port': 80,
'cidr_blocks': ['0.0.0.0/0'],
}],
egress=[{
'protocol': '-1',
'from_port': 0,
'to_port': 0,
'cidr_blocks': ['0.0.0.0/0'],
}]
)
# Create a load balancer to listen for HTTP traffic on port 80.
alb = aws.lb.LoadBalancer('app-lb',
security_groups=[group.id],
subnets=default_vpc_subnets.ids
)
atg = aws.lb.TargetGroup('app-tg',
port=80,
protocol='HTTP',
target_type='ip',
vpc_id=default_vpc.id
)
wl = aws.lb.Listener('web',
load_balancer_arn=alb.arn,
port=80,
default_actions=[{
'type': 'forward',
'target_group_arn': atg.arn
}]
)
# Create an IAM role that can be used by our service's task.
role = aws.iam.Role('task-exec-role',
assume_role_policy=json.dumps({
'Version': '2008-10-17',
'Statement': [{
'Sid': '',
'Effect': 'Allow',
'Principal': {
'Service': 'ecs-tasks.amazonaws.com'
},
'Action': 'sts:AssumeRole',
}]
})
)
rpa = aws.iam.RolePolicyAttachment('task-exec-policy',
role=role.name,
policy_arn='arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
)
# Spin up a load balanced service running our container image.
task_definition = aws.ecs.TaskDefinition('app-task',
family='fargate-task-definition',
cpu='256',
memory='512',
network_mode='awsvpc',
requires_compatibilities=['FARGATE'],
execution_role_arn=role.arn,
container_definitions=json.dumps([{
'name': 'my-app',
'image': 'nginx',
'portMappings': [{
'containerPort': 80,
'hostPort': 80,
'protocol': 'tcp'
}]
}])
)
service = aws.ecs.Service('app-svc',
cluster=cluster.arn,
desired_count=3,
launch_type='FARGATE',
task_definition=task_definition.arn,
network_configuration={
'assign_public_ip': 'true',
'subnets': default_vpc_subnets.ids,
'security_groups': [group.id]
},
load_balancers=[{
'target_group_arn': atg.arn,
'container_name': 'my-app',
'container_port': 80
}],
opts=ResourceOptions(depends_on=[wl]),
)
export('url', alb.dns_name)