Skip to content

Commit bcb7c63

Browse files
feat(aws-fargate-ssmstringparameter): New Construct (#653)
* created README * created aws-fargate-ssmstringparameter construct * revert package.json version number * added task definition check tests * fixed conflicting string name Co-authored-by: biffgaut <[email protected]>
1 parent 824bb19 commit bcb7c63

12 files changed

+3800
-0
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# aws-fargate-ssmstringparameter module
2+
<!--BEGIN STABILITY BANNER-->
3+
4+
---
5+
6+
![Stability: Experimental](https://img.shields.io/badge/stability-Experimental-important.svg?style=for-the-badge)
7+
8+
> All classes are under active development and subject to non-backward compatible changes or removal in any
9+
> future version. These are not subject to the [Semantic Versioning](https://semver.org/) model.
10+
> This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
11+
12+
---
13+
<!--END STABILITY BANNER-->
14+
15+
| **Reference Documentation**:| <span style="font-weight: normal">https://docs.aws.amazon.com/solutions/latest/constructs/</span>|
16+
|:-------------|:-------------|
17+
<div style="height:8px"></div>
18+
19+
| **Language** | **Package** |
20+
|:-------------|-----------------|
21+
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`aws_solutions_constructs.aws_fargate_ssmstringparameter`|
22+
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@aws-solutions-constructs/aws-fargate-ssmstringparameter`|
23+
|![Java Logo](https://docs.aws.amazon.com/cdk/api/latest/img/java32.png) Java|`software.amazon.awsconstructs.services.fargatessmstringparameter`|
24+
25+
This AWS Solutions Construct implements an AWS Fargate service that can read/write to an AWS Systems Manager String Parameter
26+
27+
Here is a minimal deployable pattern definition:
28+
29+
Typescript
30+
``` typescript
31+
import { Construct } from 'constructs';
32+
import { Stack, StackProps } from 'aws-cdk-lib';
33+
import { FargateToSsmstringparameter, FargateToSsmstringparameterProps } from '@aws-solutions-constructs/aws-fargate-ssmstringparameter';
34+
35+
const constructProps: FargateToSsmstringparameterProps = {
36+
publicApi: true,
37+
ecrRepositoryArn: "arn:aws:ecr:us-east-1:123456789012:repository/your-ecr-repo",
38+
stringParameterProps: { stringValue: "test-string-value" }
39+
};
40+
41+
new FargateToSsmstringparameter(stack, 'test-construct', constructProps);
42+
```
43+
44+
Python
45+
``` python
46+
from aws_solutions_constructs.aws_fargate_ssmstringparameter import FargateToSsmstringparameter, FargateToSsmstringparameterProps
47+
from aws_cdk import (
48+
Stack,
49+
aws_ssm as ssm
50+
)
51+
from constructs import Construct
52+
53+
FargateToSsmstringparameter(self, 'test_construct',
54+
public_api=True,
55+
ecr_repository_arn="arn:aws:ecr:us-east-1:123456789012:repository/your-ecr-repo",
56+
string_parameter_props=ssm.StringParameterProps(
57+
string_value="test-string-value"))
58+
```
59+
60+
Java
61+
``` java
62+
import software.constructs.Construct;
63+
64+
import software.amazon.awscdk.Stack;
65+
import software.amazon.awscdk.StackProps;
66+
import software.amazon.awscdk.services.ssm.*;
67+
import software.amazon.awsconstructs.services.fargatessmstringparameter.*;
68+
69+
new FargateToSsmstringparameter(this, "test-construct", new FargateToSsmstringparameterProps.Builder()
70+
.publicApi(true)
71+
.ecrRepositoryArn("arn:aws:ecr:us-east-1:123456789012:repository/your-ecr-repo")
72+
.stringParameterProps(new StringParameterProps.Builder()
73+
.stringValue("test-string-value")
74+
.build())
75+
.build());
76+
```
77+
78+
## Pattern Construct Props
79+
80+
| **Name** | **Type** | **Description** |
81+
|:-------------|:----------------|-----------------|
82+
| publicApi | `boolean` | Whether the construct is deploying a private or public API. This has implications for the VPC. |
83+
| vpcProps? | [`ec2.VpcProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.VpcProps.html) | Optional custom properties for a VPC the construct will create. This VPC will be used by any Private Hosted Zone the construct creates (that's why loadBalancerProps and privateHostedZoneProps can't include a VPC). Providing both this and existingVpc is an error. |
84+
| existingVpc? | [`ec2.IVpc`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.IVpc.html) | An existing VPC in which to deploy the construct. Providing both this and vpcProps is an error. If the client provides an existing load balancer and/or existing Private Hosted Zone, those constructs must exist in this VPC. |
85+
| clusterProps? | [`ecs.ClusterProps`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.ClusterProps.html) | Optional properties to create a new ECS cluster. To provide an existing cluster, use the cluster attribute of fargateServiceProps. |
86+
| ecrRepositoryArn? | `string` | The arn of an ECR Repository containing the image to use to generate the containers. Either this or the image property of containerDefinitionProps must be provided. format: arn:aws:ecr:*region*:*account number*:repository/*Repository Name* |
87+
| ecrImageVersion? | `string` | The version of the image to use from the repository. Defaults to 'Latest' |
88+
| containerDefinitionProps? | [`ecs.ContainerDefinitionProps \| any`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.ContainerDefinitionProps.html) | Optional props to define the container created for the Fargate Service (defaults found in fargate-defaults.ts) |
89+
| fargateTaskDefinitionProps? | [`ecs.FargateTaskDefinitionProps \| any`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.FargateTaskDefinitionProps.html) | Optional props to define the Fargate Task Definition for this construct (defaults found in fargate-defaults.ts) |
90+
| fargateServiceProps? | [`ecs.FargateServiceProps \| any`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.FargateServiceProps.html) | Optional values to override default Fargate Task definition properties (fargate-defaults.ts). The construct will default to launching the service is the most isolated subnets available (precedence: Isolated, Private and Public). Override those and other defaults here. |
91+
| existingFargateServiceObject? | [`ecs.FargateService`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.FargateService.html) | A Fargate Service already instantiated (probably by another Solutions Construct). If this is specified, then no props defining a new service can be provided, including: ecrImageVersion, containerDefinitionProps, fargateTaskDefinitionProps, ecrRepositoryArn, fargateServiceProps, clusterProps |
92+
| existingContainerDefinitionObject? | [`ecs.ContainerDefinition`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.ContainerDefinition.html) | A container definition already instantiated as part of a Fargate service. This must be the container in the existingFargateServiceObject |
93+
|existingStringParameterObj?|[`ssm.StringParameter`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.StringParameter.html)|Existing instance of SSM String parameter object, providing both this and `stringParameterProps` will cause an error|
94+
|stringParameterProps?|[`ssm.StringParameterProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.StringParameterProps.html)|Optional user provided props to override the default props for SSM String parameter. If existingStringParameterObj is not set stringParameterProps is required. The only supported [`ssm.StringParameterProps.type`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.StringParameterProps.html#type) is [`STRING`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.ParameterType.html#string) if a different value is provided it will be overridden.|
95+
|stringParameterPermissions?|`string`|Optional SSM String parameter permissions to grant to the Fargate service. One of the following may be specified: "Read", "ReadWrite".
96+
|stringParameterEnvironmentVariableName?|`string`|Optional Name for the SSM parameter name environment variable set for the container.|
97+
98+
## Pattern Properties
99+
100+
| **Name** | **Type** | **Description** |
101+
|:-------------|:----------------|-----------------|
102+
| vpc | [`ec2.IVpc`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.IVpc.html) | The VPC used by the construct (whether created by the construct or provided by the client) |
103+
| service | [`ecs.FargateService`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.FargateService.html) | The AWS Fargate service used by this construct (whether created by this construct or passed to this construct at initialization) |
104+
| container | [`ecs.ContainerDefinition`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.ContainerDefinition.html) | The container associated with the AWS Fargate service in the service property. |
105+
|stringParameter|[`ssm.StringParameter`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.StringParameter.html)|Returns an instance of `ssm.StringParameter` created by the construct|
106+
107+
## Default settings
108+
109+
Out of the box implementation of the Construct without any override will set the following defaults:
110+
111+
### AWS Fargate Service
112+
* Sets up an AWS Fargate service
113+
* Uses the existing service if provided
114+
* Creates a new service if none provided.
115+
* Service will run in isolated subnets if available, then private subnets if available and finally public subnets
116+
* Adds environment variables to the container with the ARN and Name of the SSM parameter
117+
* Add permissions to the container IAM role allowing it to read/write to the SSM parameter
118+
119+
### AWS SSM String Parameter
120+
* Sets up an AWS SSM String Parameter
121+
* Uses an existing parameter if one is provided, otherwise creates a new one
122+
* Adds an Interface Endpoint to the VPC for SSM parameter (the service by default runs in Isolated or Private subnets)
123+
124+
## Architecture
125+
![Architecture Diagram](architecture.png)
126+
127+
***
128+
&copy; Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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 - 'Read'
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+
this.stringParameter.grantRead(this.service.taskDefinition.taskRole);
186+
187+
if (props.stringParameterPermissions) {
188+
const _permissions = props.stringParameterPermissions.toUpperCase();
189+
190+
// Add the requested string parameter permission
191+
if (_permissions === 'READWRITE') {
192+
this.stringParameter.grantWrite(this.service.taskDefinition.taskRole);
193+
}
194+
}
195+
196+
// Add environment variables
197+
const stringParameterEnvironmentVariableName = props.stringParameterEnvironmentVariableName || 'SSM_STRING_PARAMETER_NAME';
198+
this.container.addEnvironment(stringParameterEnvironmentVariableName, this.stringParameter.parameterName);
199+
}
200+
}

0 commit comments

Comments
 (0)