Skip to content

chore: upgrade cdk-blue-green-container-deployment to v2 #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: cdk2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions packages/cdk-blue-green-container-deployment/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cloudcomponents/cdk-blue-green-container-deployment",
"version": "1.48.0",
"version": "2.0.0",
"description": "Blue green container deployment with CodeDeploy",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -60,36 +60,21 @@
}
}
},
"peerDependencies": {
"@aws-cdk/aws-codebuild": "^1.134.0",
"@aws-cdk/aws-codedeploy": "^1.134.0",
"@aws-cdk/aws-ec2": "^1.134.0",
"@aws-cdk/aws-ecr": "^1.134.0",
"@aws-cdk/aws-ecs": "^1.134.0",
"@aws-cdk/aws-elasticloadbalancingv2": "^1.134.0",
"@aws-cdk/aws-iam": "^1.134.0",
"@aws-cdk/aws-lambda": "^1.134.0",
"@aws-cdk/core": "^1.134.0",
"@aws-cdk/custom-resources": "^1.134.0",
"constructs": "^3.2.0"
},
"dependencies": {
"@aws-cdk/aws-codebuild": "^1.134.0",
"@aws-cdk/aws-codedeploy": "^1.134.0",
"@aws-cdk/aws-ec2": "^1.134.0",
"@aws-cdk/aws-ecr": "^1.134.0",
"@aws-cdk/aws-ecs": "^1.134.0",
"@aws-cdk/aws-elasticloadbalancingv2": "^1.134.0",
"@aws-cdk/aws-iam": "^1.134.0",
"@aws-cdk/aws-lambda": "^1.134.0",
"@aws-cdk/core": "^1.134.0",
"@aws-cdk/custom-resources": "^1.134.0"
"aws-cdk-lib": "2.4.0",
"aws-lambda": "^1.0.7",
"constructs": "^10.0.0",
"custom-resource-helper": "^1.0.15",
"source-map-support": "^0.5.16"
},
"devDependencies": {
"@aws-cdk/assert": "^1.134.0",
"aws-sdk": "^2.1035.0",
"custom-resource-helper": "^1.0.15",
"aws-cdk": "2.4.0",
"jest": "^26.4.2",
"jest-cdk-snapshot": "^1.4.2",
"ts-jest": "^26.2.0",
"ts-node": "^9.0.0",
"typescript": "~3.9.7",
"winston": "^3.3.3"
},
"externals": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { NetworkMode } from '@aws-cdk/aws-ecs';
import { Role, ServicePrincipal, ManagedPolicy, PolicyStatement, Effect, IRole } from '@aws-cdk/aws-iam';
import { Construct, ITaggable, TagManager, TagType, Lazy } from '@aws-cdk/core';
import { AwsCustomResource, AwsCustomResourcePolicy, AwsSdkCall, PhysicalResourceId, PhysicalResourceIdReference } from '@aws-cdk/custom-resources';

import { NetworkMode } from "aws-cdk-lib/aws-ecs";
import {
Role,
ServicePrincipal,
ManagedPolicy,
PolicyStatement,
Effect,
IRole,
} from "aws-cdk-lib/aws-iam";
import { ITaggable, TagManager, TagType, Lazy } from "aws-cdk-lib/core";
import {
AwsCustomResource,
AwsCustomResourcePolicy,
AwsSdkCall,
PhysicalResourceId,
PhysicalResourceIdReference,
} from "aws-cdk-lib/custom-resources";
import { Construct } from "constructs";
export interface IDummyTaskDefinition {
readonly executionRole: IRole;

Expand Down Expand Up @@ -40,7 +53,10 @@ export interface DummyTaskDefinitionProps {
readonly containerPort?: number;
}

export class DummyTaskDefinition extends Construct implements IDummyTaskDefinition, ITaggable {
export class DummyTaskDefinition
extends Construct
implements IDummyTaskDefinition, ITaggable
{
public readonly executionRole: IRole;

public readonly family: string;
Expand All @@ -56,73 +72,84 @@ export class DummyTaskDefinition extends Construct implements IDummyTaskDefiniti
constructor(scope: Construct, id: string, props: DummyTaskDefinitionProps) {
super(scope, id);

this.tags = new TagManager(TagType.STANDARD, 'TagManager');
this.tags = new TagManager(TagType.STANDARD, "TagManager");

this.executionRole = new Role(this, 'ExecutionRole', {
assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'),
managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonECSTaskExecutionRolePolicy')],
this.executionRole = new Role(this, "ExecutionRole", {
assumedBy: new ServicePrincipal("ecs-tasks.amazonaws.com"),
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName(
"service-role/AmazonECSTaskExecutionRolePolicy"
),
],
});

this.family = props.family ?? this.node.addr;
this.containerName = props.containerName ?? 'sample-website';
this.containerName = props.containerName ?? "sample-website";
this.containerPort = props.containerPort ?? 80;

const registerTaskDefinition: AwsSdkCall = {
service: 'ECS',
action: 'registerTaskDefinition',
service: "ECS",
action: "registerTaskDefinition",
parameters: {
requiresCompatibilities: ['FARGATE'],
requiresCompatibilities: ["FARGATE"],
family: this.family,
executionRoleArn: this.executionRole.roleArn,
networkMode: NetworkMode.AWS_VPC,
cpu: '256',
memory: '512',
cpu: "256",
memory: "512",
containerDefinitions: [
{
name: this.containerName,
image: props.image,
portMappings: [
{
hostPort: this.containerPort,
protocol: 'tcp',
protocol: "tcp",
containerPort: this.containerPort,
},
],
},
],
tags: Lazy.any({ produce: () => this.tags.renderTags() }),
},
physicalResourceId: PhysicalResourceId.fromResponse('taskDefinition.taskDefinitionArn'),
physicalResourceId: PhysicalResourceId.fromResponse(
"taskDefinition.taskDefinitionArn"
),
};

const deregisterTaskDefinition: AwsSdkCall = {
service: 'ECS',
action: 'deregisterTaskDefinition',
service: "ECS",
action: "deregisterTaskDefinition",
parameters: {
taskDefinition: new PhysicalResourceIdReference(),
},
};

const taskDefinition = new AwsCustomResource(this, 'DummyTaskDefinition', {
resourceType: 'Custom::DummyTaskDefinition',
const taskDefinition = new AwsCustomResource(this, "DummyTaskDefinition", {
resourceType: "Custom::DummyTaskDefinition",
onCreate: registerTaskDefinition,
onUpdate: registerTaskDefinition,
onDelete: deregisterTaskDefinition,
policy: AwsCustomResourcePolicy.fromStatements([
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['ecs:RegisterTaskDefinition', 'ecs:DeregisterTaskDefinition'],
resources: ['*'],
actions: [
"ecs:RegisterTaskDefinition",
"ecs:DeregisterTaskDefinition",
],
resources: ["*"],
}),
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['iam:PassRole'],
actions: ["iam:PassRole"],
resources: [this.executionRole.roleArn],
}),
]),
});

this.taskDefinitionArn = taskDefinition.getResponseField('taskDefinition.taskDefinitionArn');
this.taskDefinitionArn = taskDefinition.getResponseField(
"taskDefinition.taskDefinitionArn"
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CfnDeploymentConfig } from '@aws-cdk/aws-codedeploy';
import { Aws, Construct, IResolvable, Resource } from '@aws-cdk/core';
import { CfnDeploymentConfig } from "aws-cdk-lib/aws-codedeploy";
import { Aws, IResolvable, Resource } from "aws-cdk-lib/core";
import { Construct } from "constructs";

export interface IEcsDeploymentConfig {
readonly deploymentConfigName: string;
Expand All @@ -20,22 +21,39 @@ export interface EcsDeploymentConfigurationProps {
* @external
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codedeploy-deploymentconfig.html#cfn-codedeploy-deploymentconfig-minimumhealthyhosts
*/
readonly minimumHealthyHosts?: CfnDeploymentConfig.MinimumHealthyHostsProperty | IResolvable;
readonly minimumHealthyHosts?:
| CfnDeploymentConfig.MinimumHealthyHostsProperty
| IResolvable;
/**
* `AWS::CodeDeploy::DeploymentConfig.TrafficRoutingConfig`.
*
* @external
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codedeploy-deploymentconfig.html#cfn-codedeploy-deploymentconfig-trafficroutingconfig
*/
readonly trafficRoutingConfig?: CfnDeploymentConfig.TrafficRoutingConfigProperty | IResolvable;
readonly trafficRoutingConfig?:
| CfnDeploymentConfig.TrafficRoutingConfigProperty
| IResolvable;
}

export class EcsDeploymentConfig extends Resource implements IEcsDeploymentConfig {
public static readonly LINEAR_10PERCENT_EVERY_1MINUTE = deploymentConfig('CodeDeployDefault.ECSLinear10PercentEvery1Minutes');
public static readonly LINEAR_10PERCENT_EVERY_3MINUTES = deploymentConfig('CodeDeployDefault.ECSLinear10PercentEvery3Minutes');
public static readonly CANARY_10PERCENT_5MINUTES = deploymentConfig('CodeDeployDefault.ECSCanary10Percent5Minutes');
public static readonly CANARY_10PERCENT_15MINUTES = deploymentConfig('CodeDeployDefault.ECSCanary10Percent15Minutes');
public static readonly ALL_AT_ONCE = deploymentConfig('CodeDeployDefault.ECSAllAtOnce');
export class EcsDeploymentConfig
extends Resource
implements IEcsDeploymentConfig
{
public static readonly LINEAR_10PERCENT_EVERY_1MINUTE = deploymentConfig(
"CodeDeployDefault.ECSLinear10PercentEvery1Minutes"
);
public static readonly LINEAR_10PERCENT_EVERY_3MINUTES = deploymentConfig(
"CodeDeployDefault.ECSLinear10PercentEvery3Minutes"
);
public static readonly CANARY_10PERCENT_5MINUTES = deploymentConfig(
"CodeDeployDefault.ECSCanary10Percent5Minutes"
);
public static readonly CANARY_10PERCENT_15MINUTES = deploymentConfig(
"CodeDeployDefault.ECSCanary10Percent15Minutes"
);
public static readonly ALL_AT_ONCE = deploymentConfig(
"CodeDeployDefault.ECSAllAtOnce"
);

/**
* Import a custom Deployment Configuration for an ECS Deployment Group defined outside the CDK.
Expand All @@ -45,23 +63,37 @@ export class EcsDeploymentConfig extends Resource implements IEcsDeploymentConfi
* @param ecsDeploymentConfigName the name of the referenced custom Deployment Configuration
* @returns a Construct representing a reference to an existing custom Deployment Configuration
*/
public static fromEcsDeploymentConfigName(_scope: Construct, _id: string, ecsDeploymentConfigName: string): IEcsDeploymentConfig {
public static fromEcsDeploymentConfigName(
_scope: Construct,
_id: string,
ecsDeploymentConfigName: string
): IEcsDeploymentConfig {
return deploymentConfig(ecsDeploymentConfigName);
}

public readonly deploymentConfigName: string;
public readonly deploymentConfigArn: string;

constructor(scope: Construct, id: string, props: EcsDeploymentConfigurationProps) {
constructor(
scope: Construct,
id: string,
props: EcsDeploymentConfigurationProps
) {
super(scope, id);

const cfnDeploymentConfig = new CfnDeploymentConfig(this, 'EcsDeploymentConfiguration', {
computePlatform: 'ECS',
...props,
});
const cfnDeploymentConfig = new CfnDeploymentConfig(
this,
"EcsDeploymentConfiguration",
{
computePlatform: "ECS",
...props,
}
);

this.deploymentConfigName = cfnDeploymentConfig.ref;
this.deploymentConfigArn = arnForDeploymentConfig(this.deploymentConfigName);
this.deploymentConfigArn = arnForDeploymentConfig(
this.deploymentConfigName
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import * as path from 'path';
import { EcsApplication, IEcsApplication } from '@aws-cdk/aws-codedeploy';
import { ApplicationTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2';
import { Role, ServicePrincipal, ManagedPolicy, Effect, PolicyStatement } from '@aws-cdk/aws-iam';
import { Function, Runtime, Code } from '@aws-cdk/aws-lambda';
import { Construct, Resource, IResource, CustomResource, Duration, ITaggable, TagType, TagManager, Lazy } from '@aws-cdk/core';
import { EcsApplication, IEcsApplication } from "aws-cdk-lib/aws-codedeploy";
import { ApplicationTargetGroup } from "aws-cdk-lib/aws-elasticloadbalancingv2";
import {
Role,
ServicePrincipal,
ManagedPolicy,
Effect,
PolicyStatement,
} from "aws-cdk-lib/aws-iam";
import { Function, Runtime, Code } from "aws-cdk-lib/aws-lambda";
import {
Resource,
IResource,
CustomResource,
Duration,
ITaggable,
TagType,
TagManager,
Lazy,
} from "aws-cdk-lib/core";
import { Construct } from "constructs";

import { EcsDeploymentConfig, IEcsDeploymentConfig } from './ecs-deployment-config';
import { IEcsService } from './ecs-service';
Expand Down
29 changes: 23 additions & 6 deletions packages/cdk-blue-green-container-deployment/src/ecs-service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import * as path from 'path';
import { IConnectable, Connections, SecurityGroup, Port } from '@aws-cdk/aws-ec2';
import { ICluster, LaunchType, DeploymentCircuitBreaker } from '@aws-cdk/aws-ecs';
import { ITargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2';
import { Effect, PolicyStatement } from '@aws-cdk/aws-iam';
import { Function, Runtime, Code } from '@aws-cdk/aws-lambda';
import { Duration, Construct, CustomResource, ITaggable, TagManager, TagType, Lazy } from '@aws-cdk/core';
import {
IConnectable,
Connections,
SecurityGroup,
Port,
} from "aws-cdk-lib/aws-ec2";
import {
ICluster,
LaunchType,
DeploymentCircuitBreaker,
} from "aws-cdk-lib/aws-ecs";
import { ITargetGroup } from "aws-cdk-lib/aws-elasticloadbalancingv2";
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
import { Function, Runtime, Code } from "aws-cdk-lib/aws-lambda";
import {
Duration,
CustomResource,
ITaggable,
TagManager,
TagType,
Lazy,
} from "aws-cdk-lib/core";
import { Construct } from "constructs";

import { DummyTaskDefinition } from './dummy-task-definition';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
ComputeType,
BuildEnvironmentVariable,
BuildEnvironmentVariableType,
} from '@aws-cdk/aws-codebuild';
import { IRepository } from '@aws-cdk/aws-ecr';
import { PolicyStatement } from '@aws-cdk/aws-iam';
import { Construct, Stack } from '@aws-cdk/core';
} from "aws-cdk-lib/aws-codebuild";
import { IRepository } from "aws-cdk-lib/aws-ecr";
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
import { Stack } from "aws-cdk-lib/core";
import { Construct } from "constructs";

import { BuildSpecGenerator } from './build-spec-generator';
import { IDummyTaskDefinition } from './dummy-task-definition';
Expand Down