Amazon.EC2.Util.VPCUtilities.LaunchVPCWithPublicSubnet C# (CSharp) Method

LaunchVPCWithPublicSubnet() private static method

This method will create a VPC with a subnet that will have an internet gateway attached making instances available to the internet.
private static LaunchVPCWithPublicSubnet ( IAmazonEC2 ec2Client, Amazon.EC2.Util.LaunchVPCWithPublicSubnetRequest request, Amazon.EC2.Util.LaunchVPCWithPublicSubnetResponse response ) : void
ec2Client IAmazonEC2 The ec2client used to create the VPC
request Amazon.EC2.Util.LaunchVPCWithPublicSubnetRequest The properties used to create the VPC.
response Amazon.EC2.Util.LaunchVPCWithPublicSubnetResponse The response contains all the VPC objects that were created.
return void
        private static void LaunchVPCWithPublicSubnet(IAmazonEC2 ec2Client, LaunchVPCWithPublicSubnetRequest request, LaunchVPCWithPublicSubnetResponse response)
        {
            response.VPC = ec2Client.CreateVpc(new CreateVpcRequest()
            {
                CidrBlock = request.VPCCidrBlock,
                InstanceTenancy = request.InstanceTenancy
            }).Vpc;
            WriteProgress(request.ProgressCallback, "Created vpc {0}", response.VPC.VpcId);

            var describeVPCRequest = new DescribeVpcsRequest() { VpcIds = new List<string>() { response.VPC.VpcId } };
            WaitTillTrue(((Func<bool>)(() => ec2Client.DescribeVpcs(describeVPCRequest).Vpcs.Count == 1)));

            if(!string.IsNullOrEmpty(request.VPCName))
            {
                ec2Client.CreateTags(new CreateTagsRequest()
                {
                    Resources = new List<string>(){ response.VPC.VpcId}, 
                    Tags = new List<Tag>(){new Tag(){Key = "Name", Value = request.VPCName}}
                });
            }

            response.PublicSubnet = ec2Client.CreateSubnet(new CreateSubnetRequest()
            {
                AvailabilityZone = request.PublicSubnetAvailabilityZone,
                CidrBlock = request.PublicSubnetCiderBlock,
                VpcId = response.VPC.VpcId
            }).Subnet;
            WriteProgress(request.ProgressCallback, "Created public subnet {0}", response.PublicSubnet.SubnetId);

            WaitTillTrue(((Func<bool>)(() => (ec2Client.DescribeSubnets(new DescribeSubnetsRequest() { SubnetIds = new List<string>() { response.PublicSubnet.SubnetId } }).Subnets.Count == 1))));

            ec2Client.CreateTags(new CreateTagsRequest()
            {
                Resources = new List<string>() { response.PublicSubnet.SubnetId },
                Tags = new List<Tag>() { new Tag() { Key = "Name", Value = "Public" } }
            });

            response.InternetGateway = ec2Client.CreateInternetGateway(new CreateInternetGatewayRequest()
            {
            }).InternetGateway;
            WriteProgress(request.ProgressCallback, "Created internet gateway {0}", response.InternetGateway.InternetGatewayId);

            ec2Client.AttachInternetGateway(new AttachInternetGatewayRequest()
            {
                InternetGatewayId = response.InternetGateway.InternetGatewayId,
                VpcId = response.VPC.VpcId
            });
            WriteProgress(request.ProgressCallback, "Attached internet gateway to vpc");

            response.PublicSubnetRouteTable = ec2Client.CreateRouteTable(new CreateRouteTableRequest()
            {
                VpcId = response.VPC.VpcId
            }).RouteTable;
            WriteProgress(request.ProgressCallback, "Created route table {0}", response.PublicSubnetRouteTable.RouteTableId);

            var describeRouteTableRequest = new DescribeRouteTablesRequest() { RouteTableIds = new List<string>() { response.PublicSubnetRouteTable.RouteTableId } };
            WaitTillTrue(((Func<bool>)(() => (ec2Client.DescribeRouteTables(describeRouteTableRequest).RouteTables.Count == 1))));

            ec2Client.CreateTags(new CreateTagsRequest()
            {
                Resources = new List<string>() { response.PublicSubnetRouteTable.RouteTableId },
                Tags = new List<Tag>() { new Tag() { Key = "Name", Value = "Public" } }
            });

            ec2Client.AssociateRouteTable(new AssociateRouteTableRequest()
            {
                RouteTableId = response.PublicSubnetRouteTable.RouteTableId,
                SubnetId = response.PublicSubnet.SubnetId
            });
            WriteProgress(request.ProgressCallback, "Associated route table to public subnet");

            ec2Client.CreateRoute(new CreateRouteRequest()
            {
                DestinationCidrBlock = "0.0.0.0/0",
                GatewayId = response.InternetGateway.InternetGatewayId,
                RouteTableId = response.PublicSubnetRouteTable.RouteTableId
            });
            WriteProgress(request.ProgressCallback, "Added route for internet gateway to route table {0}", response.PublicSubnetRouteTable.RouteTableId);

            response.PublicSubnetRouteTable = ec2Client.DescribeRouteTables(describeRouteTableRequest).RouteTables[0];
        }

Same methods

VPCUtilities::LaunchVPCWithPublicSubnet ( IAmazonEC2 ec2Client, Amazon.EC2.Util.LaunchVPCWithPublicSubnetRequest request ) : Amazon.EC2.Util.LaunchVPCWithPublicSubnetResponse