Skip to content

Instantly share code, notes, and snippets.

@OksanaH
Last active Dec 29, 2020
Embed
What would you like to do?
FargateStack.cs
using Amazon.CDK;
using Amazon.CDK.AWS.EC2;
using Amazon.CDK.AWS.ECR;
using Amazon.CDK.AWS.ECS;
using Amazon.CDK.AWS.ECS.Patterns;
namespace Fargate
{
public class RandomNumStack : Stack
{
internal RandomNumStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
var vpc = new Vpc(this, "VPC");
var cluster = new Cluster(this, "Cluster", new ClusterProps{
Vpc = vpc,
ClusterName = "RandomNumGeneratorAppCluster"
});
var taskDefinition = new FargateTaskDefinition(this, "RandomNumGeneratorTaskDefinition", new FargateTaskDefinitionProps
{
Family = "RandomNumGeneratorTaskDefinition"
});
var repo = Repository.FromRepositoryName(this, "RandomNumGeneratorECR", "number-generator");
var logging = new AwsLogDriver(new AwsLogDriverProps() {
StreamPrefix = "RandomNumGeneratorApp",
LogRetention = Amazon.CDK.AWS.Logs.RetentionDays.ONE_DAY
});
var containerOptions = new ContainerDefinitionOptions
{
Image = ContainerImage.FromEcrRepository(repo),
Logging = logging
};
var portMapping = new PortMapping()
{
ContainerPort = 80,
HostPort = 80
};
taskDefinition.AddContainer("RandomNumGeneratorAppContainer", containerOptions).AddPortMappings(portMapping);
var serviceProps = new ApplicationLoadBalancedFargateServiceProps()
{
MemoryLimitMiB = 512,
Cpu = 256,
TaskDefinition = taskDefinition,
ServiceName = "RandomNumGeneratorAppService",
Cluster = cluster
};
ApplicationLoadBalancedFargateService service = new ApplicationLoadBalancedFargateService(this, "RandomNumGeneratorAppService", serviceProps);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment