Skip to content

Instantly share code, notes, and snippets.

Avatar
📦
Bin packing containers onto hosts

Nathan Peck nathanpeck

📦
Bin packing containers onto hosts
View GitHub Profile
@nathanpeck
nathanpeck / speed-ecs-deploy.ts
Created Aug 29, 2019
An AWS CDK deployment to AWS Fargte, optimized for speed
View speed-ecs-deploy.ts
import ec2 = require('@aws-cdk/aws-ec2');
import ecs = require('@aws-cdk/aws-ecs');
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import cdk = require('@aws-cdk/core');
class PublicFargateService extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);
// Create VPC and Fargate Cluster
@nathanpeck
nathanpeck / uninstall-ecs-anywhere.sh
Created Aug 16, 2021
Uninstall ECS Anywhere from a host, for reinstall, or registration with a different account/region/cluster
View uninstall-ecs-anywhere.sh
apt remove -y amazon-ssm-agent amazon-ecs-init
rm -rf /var/lib/amazon/ssm/Vault/Store/RegistrationKey
rm -rf /var/lib/ecs/ecs.config
rm -rf /etc/ecs/ecs.config
rm -rf /var/lib/ecs/data/*
View rough-load-test.js
var request = require('request');
var async = require('async');
var faker = require('faker');
var _ = require('lodash');
if (!process.env.PEAK_PERIOD_MINS) {
process.env.PEAK_PERIOD_MINS = '30';
}
var PEAK_PERIOD_MINS = parseInt(process.env.PEAK_PERIOD_MINS, 10) * 60 * 1000;
var MAX_CONCURRENT = parseInt(process.env.MAX_CONCURRENT, 10) || 100;
View datadog-fargate-task.yml
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: my-app
Cpu: 512
Memory: 1024
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
ExecutionRoleArn: <execution role>
View x-ray-instrument-custom-namespace.js
// This handler function gets called for each SQS message that arrives in my process
mySqsHandler = function(msg) {
var self = this;
if (!msg.Attributes || !msg.Attributes.AWSTraceHeader) {
// This message was not selected for tracing by the sampler in
// a higher level service
runMySqsLogic(msg, function() {
// Noop, no segment to run
});
View scaling-policy.yml
# Enable autoscaling for the service
ScalableTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
DependsOn: Service
Properties:
ServiceNamespace: 'ecs'
ScalableDimension: 'ecs:service:DesiredCount'
ResourceId:
Fn::Join:
- '/'
View gist.js
export class MyEcsConstructStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = new ec2.VpcNetwork(this, 'MyVpc', {
maxAZs: 3 // Default is all AZs in region
});
const cluster = new ecs.Cluster(this, 'MyCluster', {
vpc: vpc
View multiple-ports-per-ecs-service.md

The information below was written in Oct 2017. In August 2019 AWS launched official support for multiple target groups per AWS ECS service. Please use that feature instead!


Unfortunately as of writing this (Oct 18, 2017) there is no built in integration for multiple target groups per AWS ECS service. Here are a few things you can try:

  1. If your application just serves port 80 (HTTP) & port 443 (HTTPS) then you should consider using the application load balancer and terminating SSL at the load balancer. This will allow your application to function using just port 80.
View cdk-prepare-method.ts
export class Service extends cdk.Construct implements ServiceInterface {
public addons: Map<string, ServiceAddon>;
protected taskDefinition: ecs.Ec2TaskDefinition;
protected service: ecs.Ec2Service;
readonly scope: cdk.Stack;
readonly id: string;
readonly vpc: ec2.Vpc;
readonly cluster: ecs.Cluster;
constructor(scope: cdk.Stack, id: string, props: ServiceProps) {
View targetgroup-stickiness.yml
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 6
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 2
TargetType: ip
Name: !Ref 'ServiceName'