Skip to content

Instantly share code, notes, and snippets.

@ztnel
Forked from mikeapted/Dockerfile
Created April 15, 2020 14:01
Show Gist options
  • Save ztnel/7d5b496cadd8329b5d56dc281bda2de2 to your computer and use it in GitHub Desktop.
Save ztnel/7d5b496cadd8329b5d56dc281bda2de2 to your computer and use it in GitHub Desktop.
cdk --version
cdk doctor
mkdir cdk-io-demo
cd cdk-io-demo
cdk init app --language=typescript
npm i @aws-cdk/aws-ec2 @aws-cdk/aws-ecs @aws-cdk/aws-ecs-patterns
npm run build
cdk synth | more
## Update files
cdk synth | more
cdk diff | more
cdk deploy
## Add autoscaling
## Update to local Dockerfile
mkdir my-container
[
{
"portMappings": [
{
"hostPort": 80,
"protocol": "tcp",
"containerPort": 80
}
],
"image": "nginx:latest",
"name": "api",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "${log_group}",
"awslogs-region": "${region}",
"awslogs-stream-prefix": "ecs"
}
}
}
]
import * as cdk from '@aws-cdk/core';
import ec2 = require('@aws-cdk/aws-ec2');
import ecs = require('@aws-cdk/aws-ecs');
import ecs_patterns = require('@aws-cdk/aws-ecs-patterns');
import path = require('path');
export class CdkIoDemoStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 2 });
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
const myService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "my-service", {
cluster,
memoryLimitMiB: 1024,
cpu: 512,
desiredCount: 2,
taskImageOptions: {
//image: ecs.ContainerImage.fromAsset(path.resolve(__dirname, '../my-container'))
image: ecs.ContainerImage.fromRegistry("nginx")
}
});
const scalableTarget = myService.service.autoScaleTaskCount({
minCapacity: 2,
maxCapacity: 10,
});
scalableTarget.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 50,
});
}
}
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { CdkIoDemoStack } from '../lib/cdk-io-demo-stack';
const app = new cdk.App();
new CdkIoDemoStack(app, 'CdkIoDemoStack', {
env: {
region: 'us-east-1',
account: 'XXXXXXXXXXXXX'
}
});
FROM nginx:alpine
RUN echo "My app!" > /usr/share/nginx/html/index.html
terraform {
required_version = "~> 0.12"
}
provider "aws" {
version = "~> 2.12.0"
region = "us-east-1"
}
module "fargate" {
source = "strvcom/fargate/aws"
name = "nginx-example"
services = {
api = {
task_definition = "api.json"
container_port = 80
cpu = "256"
memory = "512"
replicas = 2
}
}
codepipeline_events_enabled = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment