Last active
March 11, 2022 12:32
-
-
Save guitarrapc/a853cc9444d17993d4b7c047bc5b8976 to your computer and use it in GitHub Desktop.
Amazon Application Elastic Load Balancing (ELB v2) C# Sample with HTTPS -> HTTP (HTTPS by ACM Certificate)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Amazon.CertificateManager | |
using Amazon.EC2 | |
using Amazon.ElasticLoadBalancingV2 | |
using Amazon.ElasticLoadBalancingV2.Model | |
using System.Net.Http | |
using System.Threading.Tasks | |
using System.Windows.Forms.DataVisualization.Charting | |
public class Program | |
{ | |
async Task Main() | |
{ | |
var vpcTagName = "INPUT VPC TAG NAME"; | |
var targetGroupName = "INPUT TARGET GROUP NAME"; | |
var elbName = "INPUT ELB NAME"; | |
var securityGroupName = "INPUT SECURITY GROUP TAG NAME"; | |
var subnetGroupName = "INPUT SUBNET GROUP TAG NAME"; | |
var acmDomainName = "INPUT ACM DOMAIN NAME"; | |
var region = Amazon.RegionEndpoint.APNortheast1; // SELECT REGION ENDPOINT | |
using (var acmClient = new Amazon.CertificateManager.AmazonCertificateManagerClient(region)) | |
using (var ec2Client = new Amazon.EC2.AmazonEC2Client(region)) | |
using (var elbClient = new Amazon.ElasticLoadBalancingV2.AmazonElasticLoadBalancingV2Client(region)) | |
{ | |
var targetVpc = (await ec2Client.DescribeVpcsAsync()).Vpcs.Where(x => x.Tags.FirstOrDefault(tag => tag.Key == "Name").Value == vpcTagName).FirstOrDefault(); | |
// Craete TargetGroups | |
var target = (await elbClient.DescribeTargetGroupsAsync(new DescribeTargetGroupsRequest())).TargetGroups.FirstOrDefault(x => x.TargetGroupName == targetGroupName && x.Protocol == ProtocolEnum.HTTP && x.Port == 80); | |
if (target == null) | |
{ | |
await elbClient.CreateTargetGroupAsync(new CreateTargetGroupRequest | |
{ | |
Protocol = ProtocolEnum.HTTP, | |
Port = 80, | |
VpcId = targetVpc.VpcId, | |
Name = targetGroupName, | |
// HealthCheckPath = "/", | |
// HealthCheckPort = 80, | |
}); | |
} | |
// LoadBalancer | |
var loadbalancers = (await elbClient.DescribeLoadBalancersAsync(new DescribeLoadBalancersRequest())).LoadBalancers.Where(x => x.LoadBalancerName == elbName); | |
if (!loadbalancers.Any()) | |
{ | |
var securityGroups = (await ec2Client.DescribeSecurityGroupsAsync()).SecurityGroups.Where(x => x.Tags.FirstOrDefault(tag => tag.Key == "Name")?.Value == securityGroupName).Select(x => x.GroupId).ToList(); | |
var subnets = (await ec2Client.DescribeSubnetsAsync()).Subnets.Where(x => x.Tags.FirstOrDefault(tag => tag.Key == "Name")?.Value?.StartsWith(subnetGroupName) ?? false).Select(x => x.SubnetId).ToList(); | |
await elbClient.CreateLoadBalancerAsync(new CreateLoadBalancerRequest | |
{ | |
Name = elbName, | |
SecurityGroups = securityGroups, | |
Subnets = subnets, | |
Scheme = LoadBalancerSchemeEnum.InternetFacing, | |
}); | |
} | |
// Listener | |
var certificateArn = (await acmClient.ListCertificatesAsync()).CertificateSummaryList.Where(x => x.DomainName == acmDomainName).Select(x => x.CertificateArn).First(); | |
var targetGroup = (await elbClient.DescribeTargetGroupsAsync(new DescribeTargetGroupsRequest())).TargetGroups.First(x => x.TargetGroupName == targetGroupName && x.Port == 80 && x.Protocol == ProtocolEnum.HTTP); | |
var listener = await elbClient.CreateListenerAsync(new CreateListenerRequest | |
{ | |
Certificates = new List<Certificate> | |
{ | |
new Certificate | |
{ | |
CertificateArn = certificateArn, | |
} | |
}, | |
Port = 443, | |
Protocol = ProtocolEnum.HTTPS, | |
LoadBalancerArn = (await elbClient.DescribeLoadBalancersAsync(new DescribeLoadBalancersRequest())).LoadBalancers.Where(x => x.LoadBalancerName == elbName).FirstOrDefault().LoadBalancerArn, | |
DefaultActions = new List<Amazon.ElasticLoadBalancingV2.Model.Action> | |
{ | |
new Amazon.ElasticLoadBalancingV2.Model.Action | |
{ | |
TargetGroupArn = targetGroup.TargetGroupArn, | |
Type = ActionTypeEnum.Forward, | |
} | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment