Class TagParameterContainerImage
A special type of {@link ContainerImage} that uses an ECR repository for the image, but a CloudFormation Parameter for the tag of the image in that repository.
Inherited Members
Namespace: Amazon.CDK.AWS.ECS
Assembly: Amazon.CDK.AWS.ECS.dll
Syntax (csharp)
public class TagParameterContainerImage : ContainerImage
Syntax (vb)
Public Class TagParameterContainerImage
Inherits ContainerImage
Remarks
This allows providing this tag through the Parameter at deploy time, for example in a CodePipeline that pushes a new tag of the image to the repository during a build step, and then provides that new tag through the CloudFormation Parameter in the deploy step.
See: #tagParameterName
ExampleMetadata: lit=test/integ.pipeline-ecs-separate-source.lit.ts infused
Examples
/**
* These are the construction properties for `EcsAppStack`.
* They extend the standard Stack properties,
* but also require providing the ContainerImage that the service will use.
* That Image will be provided from the Stack containing the CodePipeline.
*/
class EcsAppStackProps : StackProps
{
public ContainerImage Image { get; set; }
}
/**
* This is the Stack containing a simple ECS Service that uses the provided ContainerImage.
*/
class EcsAppStack : Stack
{
public EcsAppStack(Construct scope, string id, EcsAppStackProps props) : base(scope, id, props)
{
var taskDefinition = new TaskDefinition(this, "TaskDefinition", new TaskDefinitionProps {
Compatibility = Compatibility.FARGATE,
Cpu = "1024",
MemoryMiB = "2048"
});
taskDefinition.AddContainer("AppContainer", new ContainerDefinitionOptions {
Image = props.Image
});
new FargateService(this, "EcsService", new FargateServiceProps {
TaskDefinition = taskDefinition,
Cluster = new Cluster(this, "Cluster", new ClusterProps {
Vpc = new Vpc(this, "Vpc", new VpcProps {
MaxAzs = 1
})
})
});
}
}
/**
* This is the Stack containing the CodePipeline definition that deploys an ECS Service.
*/
class PipelineStack : Stack
{
public TagParameterContainerImage TagParameterContainerImage { get; }
public PipelineStack(Construct scope, string id, StackProps? props=null) : base(scope, id, props)
{
/* ********** ECS part **************** */
// this is the ECR repository where the built Docker image will be pushed
var appEcrRepo = new Repository(this, "EcsDeployRepository");
// the build that creates the Docker image, and pushes it to the ECR repo
var appCodeDockerBuild = new PipelineProject(this, "AppCodeDockerImageBuildAndPushProject", new PipelineProjectProps {
Environment = new BuildEnvironment {
// we need to run Docker
Privileged = true
},
BuildSpec = BuildSpec.FromObject(new Dictionary<string, object> {
{ "version", "0.2" },
{ "phases", new Dictionary<string, IDictionary<string, string[]>> {
{ "build", new Struct {
Commands = new [] { "$(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)", "docker build -t $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION ." }
} },
{ "post_build", new Struct {
Commands = new [] { "docker push $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION", "export imageTag=$CODEBUILD_RESOLVED_SOURCE_VERSION" }
} }
} },
{ "env", new Dictionary<string, string[]> {
// save the imageTag environment variable as a CodePipeline Variable
{ "exported-variables", new [] { "imageTag" } }
} }
}),
EnvironmentVariables = new Dictionary<string, BuildEnvironmentVariable> {
{ "REPOSITORY_URI", new BuildEnvironmentVariable {
Value = appEcrRepo.RepositoryUri
} }
}
});
// needed for `docker push`
appEcrRepo.GrantPullPush(appCodeDockerBuild);
// create the ContainerImage used for the ECS application Stack
TagParameterContainerImage = new TagParameterContainerImage(appEcrRepo);
var cdkCodeBuild = new PipelineProject(this, "CdkCodeBuildProject", new PipelineProjectProps {
BuildSpec = BuildSpec.FromObject(new Dictionary<string, object> {
{ "version", "0.2" },
{ "phases", new Dictionary<string, IDictionary<string, string[]>> {
{ "install", new Struct {
Commands = new [] { "npm install" }
} },
{ "build", new Struct {
Commands = new [] { "npx cdk synth --verbose" }
} }
} },
{ "artifacts", new Dictionary<string, string> {
// store the entire Cloud Assembly as the output artifact
{ "base-directory", "cdk.out" },
{ "files", "**/*" }
} }
})
});
/* ********** Pipeline part **************** */
var appCodeSourceOutput = new Artifact();
var cdkCodeSourceOutput = new Artifact();
var cdkCodeBuildOutput = new Artifact();
var appCodeBuildAction = new CodeBuildAction(new CodeBuildActionProps {
ActionName = "AppCodeDockerImageBuildAndPush",
Project = appCodeDockerBuild,
Input = appCodeSourceOutput
});
new Pipeline(this, "CodePipelineDeployingEcsApplication", new PipelineProps {
ArtifactBucket = new Bucket(this, "ArtifactBucket", new BucketProps {
RemovalPolicy = RemovalPolicy.DESTROY
}),
Stages = new [] { new StageProps {
StageName = "Source",
Actions = new [] {
// this is the Action that takes the source of your application code
new CodeCommitSourceAction(new CodeCommitSourceActionProps {
ActionName = "AppCodeSource",
Repository = new Repository(this, "AppCodeSourceRepository", new RepositoryProps { RepositoryName = "AppCodeSourceRepository" }),
Output = appCodeSourceOutput
}),
// this is the Action that takes the source of your CDK code
// (which would probably include this Pipeline code as well)
new CodeCommitSourceAction(new CodeCommitSourceActionProps {
ActionName = "CdkCodeSource",
Repository = new Repository(this, "CdkCodeSourceRepository", new RepositoryProps { RepositoryName = "CdkCodeSourceRepository" }),
Output = cdkCodeSourceOutput
}) }
}, new StageProps {
StageName = "Build",
Actions = new [] { appCodeBuildAction,
new CodeBuildAction(new CodeBuildActionProps {
ActionName = "CdkCodeBuildAndSynth",
Project = cdkCodeBuild,
Input = cdkCodeSourceOutput,
Outputs = new [] { cdkCodeBuildOutput }
}) }
}, new StageProps {
StageName = "Deploy",
Actions = new [] {
new CloudFormationCreateUpdateStackAction(new CloudFormationCreateUpdateStackActionProps {
ActionName = "CFN_Deploy",
StackName = "SampleEcsStackDeployedFromCodePipeline",
// this name has to be the same name as used below in the CDK code for the application Stack
TemplatePath = cdkCodeBuildOutput.AtPath("EcsStackDeployedInPipeline.template.json"),
AdminPermissions = true,
ParameterOverrides = new Dictionary<string, object> {
// read the tag pushed to the ECR repository from the CodePipeline Variable saved by the application build step,
// and pass it as the CloudFormation Parameter for the tag
{ TagParameterContainerImage.TagParameterName, appCodeBuildAction.Variable("imageTag") }
}
}) }
} }
});
}
}
var app = new App();
// the CodePipeline Stack needs to be created first
var pipelineStack = new PipelineStack(app, "aws-cdk-pipeline-ecs-separate-sources");
// we supply the image to the ECS application Stack from the CodePipeline Stack
// we supply the image to the ECS application Stack from the CodePipeline Stack
new EcsAppStack(app, "EcsStackDeployedInPipeline", new EcsAppStackProps {
Image = pipelineStack.TagParameterContainerImage
});
Synopsis
Constructors
TagParameterContainerImage(IRepository) | |
TagParameterContainerImage(ByRefValue) | Used by jsii to construct an instance of this class from a Javascript-owned object reference |
TagParameterContainerImage(DeputyBase.DeputyProps) | Used by jsii to construct an instance of this class from DeputyProps |
Properties
TagParameterName | Returns the name of the CloudFormation Parameter that represents the tag of the image in the ECR repository. |
TagParameterValue | Returns the value of the CloudFormation Parameter that represents the tag of the image in the ECR repository. |
Methods
Bind(Construct, ContainerDefinition) | Called when the image is used by a ContainerDefinition. |
Constructors
TagParameterContainerImage(IRepository)
TagParameterContainerImage(ByRefValue)
Used by jsii to construct an instance of this class from a Javascript-owned object reference
protected TagParameterContainerImage(ByRefValue reference)
Parameters
- reference Amazon.JSII.Runtime.Deputy.ByRefValue
The Javascript-owned object reference
TagParameterContainerImage(DeputyBase.DeputyProps)
Used by jsii to construct an instance of this class from DeputyProps
protected TagParameterContainerImage(DeputyBase.DeputyProps props)
Parameters
- props Amazon.JSII.Runtime.Deputy.DeputyBase.DeputyProps
The deputy props
Properties
TagParameterName
Returns the name of the CloudFormation Parameter that represents the tag of the image in the ECR repository.
public virtual string TagParameterName { get; }
Property Value
System.String
TagParameterValue
Returns the value of the CloudFormation Parameter that represents the tag of the image in the ECR repository.
public virtual string TagParameterValue { get; }
Property Value
System.String
Methods
Bind(Construct, ContainerDefinition)
Called when the image is used by a ContainerDefinition.
public override IContainerImageConfig Bind(Construct scope, ContainerDefinition containerDefinition)
Parameters
- scope Construct
- containerDefinition ContainerDefinition
Returns