Ejemplo n.º 1
0
        /****************************************** Route Table  ******************************************/
        public async Task <List <string> > GetRouteTablesByVpc(string vpcId)
        {
            var ret      = new List <string>();
            var request  = new DescribeRouteTablesRequest();
            var response = await client.DescribeRouteTablesAsync(request);

            foreach (var routeTable in response.RouteTables)
            {
                if (routeTable.VpcId == vpcId && routeTable.Tags.Find(o => o.Key == "Name") != null)
                {
                    ret.Add(routeTable.RouteTableId);
                }
            }
            return(ret);
        }
Ejemplo n.º 2
0
        public bool load_routetable_id()
        {
            write_log(vpc_id + " のルートデーブルを確認しています。");
            try
            {
                var client    = get_client();
                var query_req = new DescribeRouteTablesRequest();

                query_req.Filters.Add(new Filter()
                {
                    Name = "vpc-id", Values = new List <string>()
                    {
                        vpc_id
                    }
                });
                var query_res = client.DescribeRouteTables(query_req);
                routetable_id = query_res.RouteTables[0].RouteTableId;
                write_log(vpc_id + " のルートデーブルは " + routetable_id + " です");

                foreach (var row in query_res.RouteTables[0].Routes)
                {
                    if (row.GatewayId.Equals(internet_gateway_id) && row.State == RouteState.Active)
                    {
                        return(true);
                    }
                }
                set_name_tag(client, query_res.RouteTables[0].RouteTableId, Helper.build_name(setting_, "rtb"));
                write_log("インターネットゲートウェイ " + internet_gateway_id + " に " + routetable_id + " を関連付けます。");
                var update_req = new CreateRouteRequest();
                update_req.RouteTableId         = routetable_id;
                update_req.DestinationCidrBlock = "0.0.0.0/0";
                update_req.GatewayId            = internet_gateway_id;
                client.CreateRoute(update_req);
            }
            catch (Exception ex)
            {
                write_log("ERROR: " + ex.ToString());
                return(false);
            }
            return(true);
        }
Ejemplo n.º 3
0
        private static bool IsSubnetPublic(IAmazonEC2 ec2Client, string subnetID)
        {
            try
            {
                var describeRouteTablesRequest = new DescribeRouteTablesRequest();
                var filter = new Filter {
                    Name = "association.subnet-id"
                };
                filter.Values.Add(subnetID);
                describeRouteTablesRequest.Filters.Add(filter);
                var regionRoutes = ec2Client.DescribeRouteTables(describeRouteTablesRequest);
                if (regionRoutes.RouteTables.Any(routeTable => routeTable.Routes.Any(route => route.DestinationCidrBlock == "0.0.0.0/0" && !string.IsNullOrEmpty(route.GatewayId) && route.GatewayId.StartsWith("igw-"))))
                {
                    return(true);
                }
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in IsSubnetPublic() : {aex.Message}");
            }

            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This method will create a VPC with a subnet that will have an internet gateway attached making instances available to the internet.
        /// </summary>
        /// <param name="ec2Client">The ec2client used to create the VPC</param>
        /// <param name="request">The properties used to create the VPC.</param>
        /// <param name="response">The response contains all the VPC objects that were created.</param>
        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];
        }