|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +using Pulumi; |
| 7 | +using Docker = Pulumi.Docker; |
| 8 | +using Ec2 = Pulumi.Aws.Ec2; |
| 9 | +using Ecs = Pulumi.Aws.Ecs; |
| 10 | +using Ecr = Pulumi.Aws.Ecr; |
| 11 | +using Elb = Pulumi.Aws.ElasticLoadBalancingV2; |
| 12 | +using Iam = Pulumi.Aws.Iam; |
| 13 | + |
| 14 | +class Program |
| 15 | +{ |
| 16 | + static Task<int> Main() |
| 17 | + { |
| 18 | + return Deployment.RunAsync(async () => |
| 19 | + { |
| 20 | + // Read back the default VPC and public subnets, which we will use. |
| 21 | + var vpc = await Ec2.Invokes.GetVpc(new Ec2.GetVpcArgs { Default = true }); |
| 22 | + var subnet = await Ec2.Invokes.GetSubnetIds(new Ec2.GetSubnetIdsArgs { VpcId = vpc.Id }); |
| 23 | + |
| 24 | + // Create a SecurityGroup that permits HTTP ingress and unrestricted egress. |
| 25 | + var webSg = new Ec2.SecurityGroup("web-sg", new Ec2.SecurityGroupArgs |
| 26 | + { |
| 27 | + VpcId = vpc.Id, |
| 28 | + Egress = |
| 29 | + { |
| 30 | + new Ec2.Inputs.SecurityGroupEgressArgs |
| 31 | + { |
| 32 | + Protocol = "-1", |
| 33 | + FromPort = 0, |
| 34 | + ToPort = 0, |
| 35 | + CidrBlocks = { "0.0.0.0/0" }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + Ingress = |
| 39 | + { |
| 40 | + new Ec2.Inputs.SecurityGroupIngressArgs |
| 41 | + { |
| 42 | + Protocol = "tcp", |
| 43 | + FromPort = 80, |
| 44 | + ToPort = 80, |
| 45 | + CidrBlocks = { "0.0.0.0/0" }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + // Create an ECS cluster to run a container-based service. |
| 51 | + var cluster = new Ecs.Cluster("app-cluster"); |
| 52 | + |
| 53 | + // Create an IAM role that can be used by our service's task. |
| 54 | + var taskExecRole = new Iam.Role("task-exec-role", new Iam.RoleArgs |
| 55 | + { |
| 56 | + AssumeRolePolicy = @"{ |
| 57 | + ""Version"": ""2008-10-17"", |
| 58 | + ""Statement"": [{ |
| 59 | + ""Sid"": """", |
| 60 | + ""Effect"": ""Allow"", |
| 61 | + ""Principal"": { |
| 62 | + ""Service"": ""ecs-tasks.amazonaws.com"" |
| 63 | + }, |
| 64 | + ""Action"": ""sts:AssumeRole"" |
| 65 | + }] |
| 66 | +}", |
| 67 | + }); |
| 68 | + var taskExecAttach = new Iam.RolePolicyAttachment("task-exec-policy", new Iam.RolePolicyAttachmentArgs |
| 69 | + { |
| 70 | + Role = taskExecRole.Name, |
| 71 | + PolicyArn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy", |
| 72 | + }); |
| 73 | + |
| 74 | + // Create a load balancer to listen for HTTP traffic on port 80. |
| 75 | + var webLb = new Elb.LoadBalancer("web-lb", new Elb.LoadBalancerArgs |
| 76 | + { |
| 77 | + Subnets = subnet.Ids, |
| 78 | + SecurityGroups = { webSg.Id }, |
| 79 | + }); |
| 80 | + var webTg = new Elb.TargetGroup("web-tg", new Elb.TargetGroupArgs |
| 81 | + { |
| 82 | + Port = 80, |
| 83 | + Protocol = "HTTP", |
| 84 | + TargetType = "ip", |
| 85 | + VpcId = vpc.Id, |
| 86 | + }); |
| 87 | + var webListener = new Elb.Listener("web-listener", new Elb.ListenerArgs |
| 88 | + { |
| 89 | + LoadBalancerArn = webLb.Arn, |
| 90 | + Port = 80, |
| 91 | + DefaultActions = |
| 92 | + { |
| 93 | + new Elb.Inputs.ListenerDefaultActionsArgs |
| 94 | + { |
| 95 | + Type = "forward", |
| 96 | + TargetGroupArn = webTg.Arn, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + // Create a private ECR registry and build and publish our app's container image to it. |
| 102 | + var appRepo = new Ecr.Repository("app-repo"); |
| 103 | + var appRepoCreds = appRepo.RegistryId.Apply(async rid => |
| 104 | + { |
| 105 | + var creds = await Ecr.Invokes.GetCredentials(new Ecr.GetCredentialsArgs { RegistryId = rid }); |
| 106 | + var credsData = Convert.FromBase64String(creds.AuthorizationToken); |
| 107 | + return Encoding.UTF8.GetString(credsData).Split(":"); |
| 108 | + }); |
| 109 | + var image = new Docker.Image("app-img", new Docker.ImageArgs |
| 110 | + { |
| 111 | + Build = "../App", |
| 112 | + ImageName = appRepo.RepositoryUrl, |
| 113 | + Registry = new Docker.ImageRegistry |
| 114 | + { |
| 115 | + Server = appRepo.RepositoryUrl, |
| 116 | + Username = appRepoCreds.Apply(creds => creds[0]), |
| 117 | + Password = appRepoCreds.Apply(creds => creds[1]), |
| 118 | + }, |
| 119 | + }); |
| 120 | + |
| 121 | + // Spin up a load balanced service running our container image. |
| 122 | + var appTask = new Ecs.TaskDefinition("app-task", new Ecs.TaskDefinitionArgs |
| 123 | + { |
| 124 | + Family = "fargate-task-definition", |
| 125 | + Cpu = "256", |
| 126 | + Memory = "512", |
| 127 | + NetworkMode = "awsvpc", |
| 128 | + RequiresCompatibilities = { "FARGATE" }, |
| 129 | + ExecutionRoleArn = taskExecRole.Arn, |
| 130 | + ContainerDefinitions = image.ImageName.Apply(imageName => @"[{ |
| 131 | + ""name"": ""my-app"", |
| 132 | + ""image"": """ + imageName + @""", |
| 133 | + ""portMappings"": [{ |
| 134 | + ""containerPort"": 80, |
| 135 | + ""hostPort"": 80, |
| 136 | + ""protocol"": ""tcp"" |
| 137 | + }] |
| 138 | +}]"), |
| 139 | + }); |
| 140 | + var appSvc = new Ecs.Service("app-svc", new Ecs.ServiceArgs |
| 141 | + { |
| 142 | + Cluster = cluster.Arn, |
| 143 | + DesiredCount = 3, |
| 144 | + LaunchType = "FARGATE", |
| 145 | + TaskDefinition = appTask.Arn, |
| 146 | + NetworkConfiguration = new Ecs.Inputs.ServiceNetworkConfigurationArgs |
| 147 | + { |
| 148 | + AssignPublicIp = true, |
| 149 | + Subnets = subnet.Ids, |
| 150 | + SecurityGroups = { webSg.Id }, |
| 151 | + }, |
| 152 | + LoadBalancers = |
| 153 | + { |
| 154 | + new Ecs.Inputs.ServiceLoadBalancersArgs |
| 155 | + { |
| 156 | + TargetGroupArn = webTg.Arn, |
| 157 | + ContainerName = "my-app", |
| 158 | + ContainerPort = 80, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, new CustomResourceOptions { DependsOn = { webListener } }); |
| 162 | + |
| 163 | + // Export the resulting web address. |
| 164 | + return new Dictionary<string, object?> |
| 165 | + { |
| 166 | + { "url", Output.Format($"http://{webLb.DnsName}") }, |
| 167 | + }; |
| 168 | + }); |
| 169 | + } |
| 170 | +} |
0 commit comments