Skip to content

Commit 1c4f018

Browse files
committed
created aws-fargate-ssmstringparameter construct
1 parent cab2518 commit 1c4f018

11 files changed

+3606
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lib/*.js
2+
test/*.js
3+
*.d.ts
4+
coverage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
lib/*.js
2+
test/*.js
3+
*.js.map
4+
*.d.ts
5+
node_modules
6+
*.generated.ts
7+
dist
8+
.jsii
9+
10+
.LAST_BUILD
11+
.nyc_output
12+
coverage
13+
.nycrc
14+
.LAST_PACKAGE
15+
*.snk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Exclude typescript source and config
2+
*.ts
3+
tsconfig.json
4+
coverage
5+
.nyc_output
6+
*.tgz
7+
*.snk
8+
*.tsbuildinfo
9+
10+
# Include javascript files and typescript declarations
11+
!*.js
12+
!*.d.ts
13+
14+
# Exclude jsii outdir
15+
dist
16+
17+
# Include .jsii
18+
!.jsii
19+
20+
# Include .jsii
21+
!.jsii

Diff for: source/patterns/@aws-solutions-constructs/aws-fargate-ssmstringparameter/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ Python
4545
``` python
4646
from aws_solutions_constructs.aws_fargate_ssmstringparameter import FargateToSsmstringparameter, FargateToSsmstringparameterProps
4747
from aws_cdk import (
48-
Stack
48+
Stack,
49+
aws_ssm as ssm
4950
)
5051
from constructs import Construct
5152

@@ -62,6 +63,7 @@ import software.constructs.Construct;
6263

6364
import software.amazon.awscdk.Stack;
6465
import software.amazon.awscdk.StackProps;
66+
import software.amazon.awscdk.services.ssm.*;
6567
import software.amazon.awsconstructs.services.fargatessmstringparameter.*;
6668

6769
new FargateToSsmstringparameter(this, "test-construct", new FargateToSsmstringparameterProps.Builder()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/**
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
14+
import * as ec2 from "@aws-cdk/aws-ec2";
15+
import * as ssm from "@aws-cdk/aws-ssm";
16+
// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate
17+
import { Construct } from "@aws-cdk/core";
18+
import * as defaults from "@aws-solutions-constructs/core";
19+
import * as ecs from "@aws-cdk/aws-ecs";
20+
21+
export interface FargateToSsmstringparameterProps {
22+
/**
23+
* Whether the construct is deploying a private or public API. This has implications for the VPC deployed
24+
* by this construct.
25+
*
26+
* @default - none
27+
*/
28+
readonly publicApi: boolean;
29+
/**
30+
* Optional custom properties for a VPC the construct will create. This VPC will
31+
* be used by the new Fargate service the construct creates (that's
32+
* why targetGroupProps can't include a VPC). Providing
33+
* both this and existingVpc is an error. An SSM Interface
34+
* endpoint will be included in this VPC.
35+
*
36+
* @default - none
37+
*/
38+
readonly vpcProps?: ec2.VpcProps;
39+
/**
40+
* An existing VPC in which to deploy the construct. Providing both this and
41+
* vpcProps is an error. If the client provides an existing Fargate service,
42+
* this value must be the VPC where the service is running. An SSM Interface
43+
* endpoint will be added to this VPC.
44+
*
45+
* @default - none
46+
*/
47+
readonly existingVpc?: ec2.IVpc;
48+
/**
49+
* Optional properties to create a new ECS cluster
50+
*/
51+
readonly clusterProps?: ecs.ClusterProps;
52+
/**
53+
* The arn of an ECR Repository containing the image to use
54+
* to generate the containers
55+
*
56+
* format:
57+
* arn:aws:ecr:[region]:[account number]:repository/[Repository Name]
58+
*/
59+
readonly ecrRepositoryArn?: string;
60+
/**
61+
* The version of the image to use from the repository
62+
*
63+
* @default - 'latest'
64+
*/
65+
readonly ecrImageVersion?: string;
66+
/*
67+
* Optional props to define the container created for the Fargate Service
68+
*
69+
* defaults - fargate-defaults.ts
70+
*/
71+
readonly containerDefinitionProps?: ecs.ContainerDefinitionProps | any;
72+
/*
73+
* Optional props to define the Fargate Task Definition for this construct
74+
*
75+
* defaults - fargate-defaults.ts
76+
*/
77+
readonly fargateTaskDefinitionProps?: ecs.FargateTaskDefinitionProps | any;
78+
/**
79+
* Optional values to override default Fargate Task definition properties
80+
* (fargate-defaults.ts). The construct will default to launching the service
81+
* is the most isolated subnets available (precedence: Isolated, Private and
82+
* Public). Override those and other defaults here.
83+
*
84+
* defaults - fargate-defaults.ts
85+
*/
86+
readonly fargateServiceProps?: ecs.FargateServiceProps | any;
87+
/**
88+
* A Fargate Service already instantiated (probably by another Solutions Construct). If
89+
* this is specified, then no props defining a new service can be provided, including:
90+
* existingImageObject, ecrImageVersion, containerDefintionProps, fargateTaskDefinitionProps,
91+
* ecrRepositoryArn, fargateServiceProps, clusterProps, existingClusterInterface. If this value
92+
* is provided, then existingContainerDefinitionObject must be provided as well.
93+
*
94+
* @default - none
95+
*/
96+
readonly existingFargateServiceObject?: ecs.FargateService;
97+
/*
98+
* A container definition already instantiated as part of a Fargate service. This must
99+
* be the container in the existingFargateServiceObject.
100+
*
101+
* @default - None
102+
*/
103+
readonly existingContainerDefinitionObject?: ecs.ContainerDefinition;
104+
/**
105+
* Optional user provided props to override the default props for SSM String Parameter.
106+
*
107+
* @default - Default props are used
108+
*/
109+
readonly stringParameterProps?: ssm.StringParameterProps;
110+
/**
111+
* Optional user provided props to override the default props for SSM String Parameter.
112+
*
113+
* @default - None
114+
*/
115+
readonly existingStringParameterObj?: ssm.StringParameter;
116+
/**
117+
* Optional SSM String parameter permissions to grant to the Fargate service. One of the following may be specified: "Read", "ReadWrite".
118+
*
119+
* @default - 'ReadWrite'
120+
*/
121+
readonly stringParameterPermissions?: string
122+
/**
123+
* Optional Name for the SSM parameter name environment variable set for the container.
124+
*
125+
* @default - None
126+
*/
127+
readonly stringParameterEnvironmentVariableName?: string;
128+
}
129+
130+
export class FargateToSsmstringparameter extends Construct {
131+
public readonly vpc: ec2.IVpc;
132+
public readonly service: ecs.FargateService;
133+
public readonly container: ecs.ContainerDefinition;
134+
public readonly stringParameter: ssm.StringParameter;
135+
136+
constructor(scope: Construct, id: string, props: FargateToSsmstringparameterProps) {
137+
super(scope, id);
138+
defaults.CheckProps(props);
139+
defaults.CheckFargateProps(props);
140+
141+
// Other permissions for constructs are accepted as arrays, turning stringParameterPermissions into
142+
// an array to use the same validation function.
143+
if (props.stringParameterPermissions) {
144+
const allowedPermissions = ['READ', 'READWRITE'];
145+
defaults.CheckListValues(allowedPermissions, [props.stringParameterPermissions.toUpperCase()], 'stringParameterPermissions');
146+
}
147+
148+
this.vpc = defaults.buildVpc(scope, {
149+
existingVpc: props.existingVpc,
150+
defaultVpcProps: props.publicApi ? defaults.DefaultPublicPrivateVpcProps() : defaults.DefaultIsolatedVpcProps(),
151+
userVpcProps: props.vpcProps,
152+
constructVpcProps: { enableDnsHostnames: true, enableDnsSupport: true }
153+
});
154+
155+
defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.SSM);
156+
157+
if (props.existingFargateServiceObject) {
158+
this.service = props.existingFargateServiceObject;
159+
// CheckFargateProps confirms that the container is provided
160+
this.container = props.existingContainerDefinitionObject!;
161+
} else {
162+
[this.service, this.container] = defaults.CreateFargateService(
163+
scope,
164+
id,
165+
this.vpc,
166+
props.clusterProps,
167+
props.ecrRepositoryArn,
168+
props.ecrImageVersion,
169+
props.fargateTaskDefinitionProps,
170+
props.containerDefinitionProps,
171+
props.fargateServiceProps
172+
);
173+
}
174+
175+
// Setup the SSM String parameter
176+
if (props.existingStringParameterObj) {
177+
this.stringParameter = props.existingStringParameterObj;
178+
} else {
179+
if (!props.stringParameterProps) {
180+
throw new Error("existingStringParameterObj or stringParameterProps needs to be provided.");
181+
}
182+
this.stringParameter = defaults.buildSsmStringParameter(this, 'stringParameter', props.stringParameterProps);
183+
}
184+
185+
// Add the requested or default string parameter permissions
186+
this.stringParameter.grantRead(this.service.taskDefinition.taskRole);
187+
if (props.stringParameterPermissions) {
188+
const _permissions = props.stringParameterPermissions.toUpperCase();
189+
190+
if (_permissions === 'READWRITE') {
191+
this.stringParameter.grantWrite(this.service.taskDefinition.taskRole);
192+
}
193+
}
194+
195+
// Add environment variables
196+
const stringParameterEnvironmentVariableName = props.stringParameterEnvironmentVariableName || 'SSM_STRING_PARAMETER_NAME';
197+
this.container.addEnvironment(stringParameterEnvironmentVariableName, this.stringParameter.parameterName);
198+
}
199+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"name": "@aws-solutions-constructs/aws-fargate-ssmstringparameter",
3+
"version": "1.149.0",
4+
"description": "CDK Constructs for AWS Fargate to AWS SSM Parameter Store Integration",
5+
"main": "lib/index.js",
6+
"types": "lib/index.d.ts",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/awslabs/aws-solutions-constructs.git",
10+
"directory": "source/patterns/@aws-solutions-constructs/aws-fargate-ssmstringparameter"
11+
},
12+
"author": {
13+
"name": "Amazon Web Services",
14+
"url": "https://aws.amazon.com",
15+
"organization": true
16+
},
17+
"license": "Apache-2.0",
18+
"scripts": {
19+
"build": "tsc -b .",
20+
"lint": "eslint -c ../eslintrc.yml --ext=.js,.ts . && tslint --project .",
21+
"lint-fix": "eslint -c ../eslintrc.yml --ext=.js,.ts --fix .",
22+
"test": "jest --coverage",
23+
"clean": "tsc -b --clean",
24+
"watch": "tsc -b -w",
25+
"integ": "cdk-integ",
26+
"integ-no-clean": "cdk-integ --no-clean",
27+
"integ-assert": "cdk-integ-assert",
28+
"jsii": "jsii",
29+
"jsii-pacmak": "jsii-pacmak",
30+
"build+lint+test": "npm run jsii && npm run lint && npm test && npm run integ-assert",
31+
"snapshot-update": "npm run jsii && npm test -- -u && npm run integ-assert"
32+
},
33+
"jsii": {
34+
"outdir": "dist",
35+
"targets": {
36+
"java": {
37+
"package": "software.amazon.awsconstructs.services.fargatessmstringparameter",
38+
"maven": {
39+
"groupId": "software.amazon.awsconstructs",
40+
"artifactId": "fargatessmstringparameter"
41+
}
42+
},
43+
"dotnet": {
44+
"namespace": "Amazon.SolutionsConstructs.AWS.FargateSsmStringParameter",
45+
"packageId": "Amazon.SolutionsConstructs.AWS.FargateSsmStringParameter",
46+
"signAssembly": true,
47+
"iconUrl": "https://raw.githubusercontent.com/aws/aws-cdk/master/logo/default-256-dark.png"
48+
},
49+
"python": {
50+
"distName": "aws-solutions-constructs.aws-fargate-ssmstringparameter",
51+
"module": "aws_solutions_constructs.aws_fargate_ssmstringparameter"
52+
}
53+
}
54+
},
55+
"dependencies": {
56+
"@aws-cdk/core": "1.149.0",
57+
"@aws-cdk/aws-ec2": "1.149.0",
58+
"@aws-cdk/aws-ssm": "1.149.0",
59+
"@aws-cdk/aws-ecs": "1.149.0",
60+
"@aws-solutions-constructs/core": "1.149.0",
61+
"constructs": "^3.2.0"
62+
},
63+
"devDependencies": {
64+
"@aws-cdk/assert": "1.149.0",
65+
"@aws-cdk/core": "1.149.0",
66+
"@aws-cdk/aws-ec2": "1.149.0",
67+
"@aws-cdk/aws-ssm": "1.149.0",
68+
"@aws-cdk/aws-ecs": "1.149.0",
69+
"@types/jest": "^26.0.22",
70+
"@aws-solutions-constructs/core": "1.149.0",
71+
"@types/node": "^10.3.0",
72+
"constructs": "3.2.0"
73+
},
74+
"jest": {
75+
"moduleFileExtensions": [
76+
"js"
77+
],
78+
"coverageReporters": [
79+
"text",
80+
[
81+
"lcov",
82+
{
83+
"projectRoot": "../../../../"
84+
}
85+
]
86+
]
87+
},
88+
"peerDependencies": {
89+
"@aws-cdk/core": "1.149.0",
90+
"@aws-cdk/aws-ec2": "1.149.0",
91+
"@aws-cdk/aws-ssm": "1.149.0",
92+
"@aws-cdk/aws-ecs": "1.149.0",
93+
"@aws-solutions-constructs/core": "1.149.0",
94+
"constructs": "^3.2.0"
95+
},
96+
"keywords": [
97+
"aws",
98+
"cdk",
99+
"awscdk",
100+
"AWS Solutions Constructs",
101+
"Amazon Systems Manager",
102+
"Amazon SSM String Parameter",
103+
"AWS Fargate"
104+
]
105+
}

0 commit comments

Comments
 (0)