github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/testing/test/testdata/aws-fargate-pp/dotnet/aws-fargate.cs (about)

     1  using System.Collections.Generic;
     2  using System.Text.Json;
     3  using Pulumi;
     4  using Aws = Pulumi.Aws;
     5  
     6  return await Deployment.RunAsync(() => 
     7  {
     8      var vpc = Aws.Ec2.GetVpc.Invoke(new()
     9      {
    10          Default = true,
    11      });
    12  
    13      var subnets = Aws.Ec2.GetSubnetIds.Invoke(new()
    14      {
    15          VpcId = vpc.Apply(getVpcResult => getVpcResult.Id),
    16      });
    17  
    18      // Create a security group that permits HTTP ingress and unrestricted egress.
    19      var webSecurityGroup = new Aws.Ec2.SecurityGroup("webSecurityGroup", new()
    20      {
    21          VpcId = vpc.Apply(getVpcResult => getVpcResult.Id),
    22          Egress = new[]
    23          {
    24              new Aws.Ec2.Inputs.SecurityGroupEgressArgs
    25              {
    26                  Protocol = "-1",
    27                  FromPort = 0,
    28                  ToPort = 0,
    29                  CidrBlocks = new[]
    30                  {
    31                      "0.0.0.0/0",
    32                  },
    33              },
    34          },
    35          Ingress = new[]
    36          {
    37              new Aws.Ec2.Inputs.SecurityGroupIngressArgs
    38              {
    39                  Protocol = "tcp",
    40                  FromPort = 80,
    41                  ToPort = 80,
    42                  CidrBlocks = new[]
    43                  {
    44                      "0.0.0.0/0",
    45                  },
    46              },
    47          },
    48      });
    49  
    50      // Create an ECS cluster to run a container-based service.
    51      var cluster = new Aws.Ecs.Cluster("cluster");
    52  
    53      // Create an IAM role that can be used by our service's task.
    54      var taskExecRole = new Aws.Iam.Role("taskExecRole", new()
    55      {
    56          AssumeRolePolicy = JsonSerializer.Serialize(new Dictionary<string, object?>
    57          {
    58              ["Version"] = "2008-10-17",
    59              ["Statement"] = new[]
    60              {
    61                  new Dictionary<string, object?>
    62                  {
    63                      ["Sid"] = "",
    64                      ["Effect"] = "Allow",
    65                      ["Principal"] = new Dictionary<string, object?>
    66                      {
    67                          ["Service"] = "ecs-tasks.amazonaws.com",
    68                      },
    69                      ["Action"] = "sts:AssumeRole",
    70                  },
    71              },
    72          }),
    73      });
    74  
    75      var taskExecRolePolicyAttachment = new Aws.Iam.RolePolicyAttachment("taskExecRolePolicyAttachment", new()
    76      {
    77          Role = taskExecRole.Name,
    78          PolicyArn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
    79      });
    80  
    81      // Create a load balancer to listen for HTTP traffic on port 80.
    82      var webLoadBalancer = new Aws.ElasticLoadBalancingV2.LoadBalancer("webLoadBalancer", new()
    83      {
    84          Subnets = subnets.Apply(getSubnetIdsResult => getSubnetIdsResult.Ids),
    85          SecurityGroups = new[]
    86          {
    87              webSecurityGroup.Id,
    88          },
    89      });
    90  
    91      var webTargetGroup = new Aws.ElasticLoadBalancingV2.TargetGroup("webTargetGroup", new()
    92      {
    93          Port = 80,
    94          Protocol = "HTTP",
    95          TargetType = "ip",
    96          VpcId = vpc.Apply(getVpcResult => getVpcResult.Id),
    97      });
    98  
    99      var webListener = new Aws.ElasticLoadBalancingV2.Listener("webListener", new()
   100      {
   101          LoadBalancerArn = webLoadBalancer.Arn,
   102          Port = 80,
   103          DefaultActions = new[]
   104          {
   105              new Aws.ElasticLoadBalancingV2.Inputs.ListenerDefaultActionArgs
   106              {
   107                  Type = "forward",
   108                  TargetGroupArn = webTargetGroup.Arn,
   109              },
   110          },
   111      });
   112  
   113      // Spin up a load balanced service running NGINX
   114      var appTask = new Aws.Ecs.TaskDefinition("appTask", new()
   115      {
   116          Family = "fargate-task-definition",
   117          Cpu = "256",
   118          Memory = "512",
   119          NetworkMode = "awsvpc",
   120          RequiresCompatibilities = new[]
   121          {
   122              "FARGATE",
   123          },
   124          ExecutionRoleArn = taskExecRole.Arn,
   125          ContainerDefinitions = JsonSerializer.Serialize(new[]
   126          {
   127              new Dictionary<string, object?>
   128              {
   129                  ["name"] = "my-app",
   130                  ["image"] = "nginx",
   131                  ["portMappings"] = new[]
   132                  {
   133                      new Dictionary<string, object?>
   134                      {
   135                          ["containerPort"] = 80,
   136                          ["hostPort"] = 80,
   137                          ["protocol"] = "tcp",
   138                      },
   139                  },
   140              },
   141          }),
   142      });
   143  
   144      var appService = new Aws.Ecs.Service("appService", new()
   145      {
   146          Cluster = cluster.Arn,
   147          DesiredCount = 5,
   148          LaunchType = "FARGATE",
   149          TaskDefinition = appTask.Arn,
   150          NetworkConfiguration = new Aws.Ecs.Inputs.ServiceNetworkConfigurationArgs
   151          {
   152              AssignPublicIp = true,
   153              Subnets = subnets.Apply(getSubnetIdsResult => getSubnetIdsResult.Ids),
   154              SecurityGroups = new[]
   155              {
   156                  webSecurityGroup.Id,
   157              },
   158          },
   159          LoadBalancers = new[]
   160          {
   161              new Aws.Ecs.Inputs.ServiceLoadBalancerArgs
   162              {
   163                  TargetGroupArn = webTargetGroup.Arn,
   164                  ContainerName = "my-app",
   165                  ContainerPort = 80,
   166              },
   167          },
   168      }, new CustomResourceOptions
   169      {
   170          DependsOn = new[]
   171          {
   172              webListener,
   173          },
   174      });
   175  
   176      return new Dictionary<string, object?>
   177      {
   178          ["url"] = webLoadBalancer.DnsName,
   179      };
   180  });
   181