-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
3,558 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
source/patterns/@aws-solutions-constructs/aws-fargate-stepfunctions/.eslintignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
lib/*.js | ||
test/*.js | ||
*.d.ts | ||
coverage |
15 changes: 15 additions & 0 deletions
15
source/patterns/@aws-solutions-constructs/aws-fargate-stepfunctions/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
lib/*.js | ||
test/*.js | ||
*.js.map | ||
*.d.ts | ||
node_modules | ||
*.generated.ts | ||
dist | ||
.jsii | ||
|
||
.LAST_BUILD | ||
.nyc_output | ||
coverage | ||
.nycrc | ||
.LAST_PACKAGE | ||
*.snk |
21 changes: 21 additions & 0 deletions
21
source/patterns/@aws-solutions-constructs/aws-fargate-stepfunctions/.npmignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Exclude typescript source and config | ||
*.ts | ||
tsconfig.json | ||
coverage | ||
.nyc_output | ||
*.tgz | ||
*.snk | ||
*.tsbuildinfo | ||
|
||
# Include javascript files and typescript declarations | ||
!*.js | ||
!*.d.ts | ||
|
||
# Exclude jsii outdir | ||
dist | ||
|
||
# Include .jsii | ||
!.jsii | ||
|
||
# Include .jsii | ||
!.jsii |
141 changes: 141 additions & 0 deletions
141
source/patterns/@aws-solutions-constructs/aws-fargate-stepfunctions/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# aws-fargate-stepfunctions module | ||
<!--BEGIN STABILITY BANNER--> | ||
|
||
--- | ||
|
||
![Stability: Experimental](https://img.shields.io/badge/stability-Experimental-important.svg?style=for-the-badge) | ||
|
||
> All classes are under active development and subject to non-backward compatible changes or removal in any | ||
> future version. These are not subject to the [Semantic Versioning](https://semver.org/) model. | ||
> 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. | ||
--- | ||
<!--END STABILITY BANNER--> | ||
|
||
| **Reference Documentation**:| <span style="font-weight: normal">https://docs.aws.amazon.com/solutions/latest/constructs/</span>| | ||
|:-------------|:-------------| | ||
<div style="height:8px"></div> | ||
|
||
| **Language** | **Package** | | ||
|:-------------|-----------------| | ||
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`aws_solutions_constructs.aws_fargate_stepfunctions`| | ||
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@aws-solutions-constructs/aws-fargate-stepfunctions`| | ||
|![Java Logo](https://docs.aws.amazon.com/cdk/api/latest/img/java32.png) Java|`software.amazon.awsconstructs.services.fargatestepfunctions`| | ||
|
||
This AWS Solutions Construct implements an AWS Fargate service that can execute an AWS Step Functions state machine | ||
|
||
Here is a minimal deployable pattern definition: | ||
|
||
Typescript | ||
``` typescript | ||
import { Construct } from 'constructs'; | ||
import { Stack, StackProps } from 'aws-cdk-lib'; | ||
import { FargateToStepfunctions, FargateToStepfunctionsProps } from '@aws-solutions-constructs/aws-fargate-stepfunctions'; | ||
import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions'; | ||
|
||
const startState = new stepfunctions.Pass(this, 'StartState'); | ||
|
||
const constructProps: FargateToStepfunctionsProps = { | ||
publicApi: true, | ||
ecrRepositoryArn: "arn:aws:ecr:us-east-1:123456789012:repository/your-ecr-repo", | ||
stateMachineProps: { | ||
definition: startState | ||
} | ||
}; | ||
|
||
new FargateToStepfunctions(this, 'test-construct', constructProps); | ||
``` | ||
|
||
Python | ||
``` python | ||
from aws_solutions_constructs.aws_fargate_stepfunctions import FargateToStepfunctions, FargateToStepfunctionsProps | ||
from aws_cdk import ( | ||
aws_stepfunctions as stepfunctions, | ||
Stack | ||
) | ||
from constructs import Construct | ||
|
||
start_state = stepfunctions.Pass(self, 'start_state') | ||
|
||
FargateToStepfunctions(self, 'test_construct', | ||
public_api=True, | ||
ecr_repository_arn="arn:aws:ecr:us-east-1:123456789012:repository/your-ecr-repo", | ||
state_machine_props=stepfunctions.StateMachineProps( | ||
definition=start_state)) | ||
``` | ||
|
||
Java | ||
``` java | ||
import software.constructs.Construct; | ||
|
||
import software.amazon.awscdk.Stack; | ||
import software.amazon.awscdk.StackProps; | ||
import software.amazon.awsconstructs.services.fargatestepfunctions.*; | ||
import software.amazon.awscdk.services.stepfunctions.*; | ||
|
||
start_state = stepfunctions.Pass(self, 'start_state') | ||
|
||
new FargateToStepfunctions(this, "test-construct", new FargateToStepfunctionsProps.Builder() | ||
.publicApi(true) | ||
.ecrRepositoryArn("arn:aws:ecr:us-east-1:123456789012:repository/your-ecr-repo") | ||
.stateMachineProps(new StateMachineProps.Builder() | ||
.definition(startState) | ||
.build() | ||
.build()); | ||
``` | ||
|
||
## Pattern Construct Props | ||
|
||
| **Name** | **Type** | **Description** | | ||
|:-------------|:----------------|-----------------| | ||
| publicApi | `boolean` | Whether the construct is deploying a private or public API. This has implications for the VPC. | | ||
| 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. | | ||
| 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. | | ||
| 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. | | ||
| 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* | | ||
| ecrImageVersion? | `string` | The version of the image to use from the repository. Defaults to 'Latest' | | ||
| 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) | | ||
| 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) | | ||
| 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. | | ||
|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 | | ||
|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 | | ||
|stateMachineProps|[`sfn.StateMachineProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-stepfunctions.StateMachineProps.html)|User provided props to override the default props for sfn.StateMachine.| | ||
| createCloudWatchAlarms? | `boolean`|Whether to create recommended CloudWatch alarms. Default is true.| | ||
|logGroupProps?|[`logs.LogGroupProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.LogGroupProps.html)|Optional user provided props to override the default props for for the CloudWatchLogs LogGroup.| | ||
|stateMachineEnvironmentVariableName?|`string`|Optional name for the container environment variable containing the state machine ARN.| | ||
## Pattern Properties | ||
| **Name** | **Type** | **Description** | | ||
|:-------------|:----------------|-----------------| | ||
| 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) | | ||
| 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) | | ||
| 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. | | ||
| stateMachine| [`sfn.StateMachine`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-stepfunctions.StateMachine.html)|Returns an instance of `sfn.StateMachine` created by the construct.| | ||
| stateMachineLogGroup|[`logs.ILogGroup`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.ILogGroup.html)|Returns an instance of the `logs.ILogGroup` created by the construct for StateMachine.| | ||
| cloudwatchAlarms? | [`cloudwatch.Alarm[]`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudwatch.Alarm.html)|Returns a list of `cloudwatch.Alarm` created by the construct.| | ||
## Default settings | ||
Out of the box implementation of the Construct without any override will set the following defaults: | ||
### AWS Fargate Service | ||
* Sets up an AWS Fargate service | ||
* Uses the existing service if provided | ||
* Creates a new service if none provided. | ||
* Service will run in isolated subnets if available, then private subnets if available and finally public subnets | ||
* Adds an environment variable to the container containing the ARN of the state machine | ||
* Default name is `STATE_MACHINE_ARN` | ||
* Add permissions to the container IAM role allowing it to start the execution of a state machine | ||
### AWS Step Functions | ||
* Sets up an AWS Step Functions state machine | ||
* Uses an existing state machine if one is provided, otherwise creates a new one | ||
* Adds an Interface Endpoint to the VPC for Step Functions (the service by default runs in Isolated or Private subnets) | ||
* Enables CloudWatch logging | ||
## Architecture | ||
![Architecture Diagram](architecture.png) | ||
*** | ||
© Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
Binary file added
BIN
+183 KB
...e/patterns/@aws-solutions-constructs/aws-fargate-stepfunctions/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
187 changes: 187 additions & 0 deletions
187
source/patterns/@aws-solutions-constructs/aws-fargate-stepfunctions/lib/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/** | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate | ||
import { Construct } from "@aws-cdk/core"; | ||
import * as defaults from "@aws-solutions-constructs/core"; | ||
import * as ecs from "@aws-cdk/aws-ecs"; | ||
import * as ec2 from "@aws-cdk/aws-ec2"; | ||
import * as sfn from '@aws-cdk/aws-stepfunctions'; | ||
import * as logs from '@aws-cdk/aws-logs'; | ||
import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; | ||
|
||
export interface FargateToStepfunctionsProps { | ||
/** | ||
* Whether the construct is deploying a private or public API. This has implications for the VPC deployed | ||
* by this construct. | ||
* | ||
* @default - none | ||
*/ | ||
readonly publicApi: boolean; | ||
/** | ||
* Optional custom properties for a VPC the construct will create. This VPC will | ||
* be used by the new Fargate service the construct creates (that's | ||
* why targetGroupProps can't include a VPC). Providing | ||
* both this and existingVpc is an error. A Step Functions Interface | ||
* endpoint will be included in this VPC. | ||
* | ||
* @default - A set of defaults from vpc-defaults.ts: DefaultPublicPrivateVpcProps() for public APIs | ||
* and DefaultIsolatedVpcProps() for private APIs. | ||
*/ | ||
readonly vpcProps?: ec2.VpcProps; | ||
/** | ||
* An existing VPC in which to deploy the construct. Providing both this and | ||
* vpcProps is an error. If the client provides an existing Fargate service, | ||
* this value must be the VPC where the service is running. A Step Functions Interface | ||
* endpoint will be added to this VPC. | ||
* | ||
* @default - none | ||
*/ | ||
readonly existingVpc?: ec2.IVpc; | ||
/** | ||
* Optional properties to create a new ECS cluster | ||
*/ | ||
readonly clusterProps?: ecs.ClusterProps; | ||
/** | ||
* The arn of an ECR Repository containing the image to use | ||
* to generate the containers | ||
* | ||
* format: | ||
* arn:aws:ecr:[region]:[account number]:repository/[Repository Name] | ||
*/ | ||
readonly ecrRepositoryArn?: string; | ||
/** | ||
* The version of the image to use from the repository | ||
* | ||
* @default - 'latest' | ||
*/ | ||
readonly ecrImageVersion?: string; | ||
/* | ||
* Optional props to define the container created for the Fargate Service | ||
* | ||
* defaults - fargate-defaults.ts | ||
*/ | ||
readonly containerDefinitionProps?: ecs.ContainerDefinitionProps | any; | ||
/* | ||
* Optional props to define the Fargate Task Definition for this construct | ||
* | ||
* defaults - fargate-defaults.ts | ||
*/ | ||
readonly fargateTaskDefinitionProps?: ecs.FargateTaskDefinitionProps | any; | ||
/** | ||
* 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. | ||
* | ||
* defaults - fargate-defaults.ts | ||
*/ | ||
readonly fargateServiceProps?: ecs.FargateServiceProps | any; | ||
/** | ||
* 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: | ||
* existingImageObject, ecrImageVersion, containerDefintionProps, fargateTaskDefinitionProps, | ||
* ecrRepositoryArn, fargateServiceProps, clusterProps, existingClusterInterface. If this value | ||
* is provided, then existingContainerDefinitionObject must be provided as well. | ||
* | ||
* @default - none | ||
*/ | ||
readonly existingFargateServiceObject?: ecs.FargateService; | ||
/* | ||
* A container definition already instantiated as part of a Fargate service. This must | ||
* be the container in the existingFargateServiceObject. | ||
* | ||
* @default - None | ||
*/ | ||
readonly existingContainerDefinitionObject?: ecs.ContainerDefinition; | ||
/** | ||
* User provided StateMachineProps to override the defaults | ||
* | ||
* @default - None | ||
*/ | ||
readonly stateMachineProps: sfn.StateMachineProps; | ||
/** | ||
* Whether to create recommended CloudWatch alarms | ||
* | ||
* @default - true | ||
*/ | ||
readonly createCloudWatchAlarms?: boolean; | ||
/** | ||
* User provided props to override the default props for the CloudWatchLogs LogGroup. | ||
* | ||
* @default - Default props are used | ||
*/ | ||
readonly logGroupProps?: logs.LogGroupProps; | ||
/** | ||
* Optional name for the container environment variable containing the state machine ARN. | ||
* | ||
* @default - None | ||
*/ | ||
readonly stateMachineEnvironmentVariableName?: string; | ||
} | ||
|
||
export class FargateToStepfunctions extends Construct { | ||
public readonly vpc: ec2.IVpc; | ||
public readonly service: ecs.FargateService; | ||
public readonly container: ecs.ContainerDefinition; | ||
public readonly stateMachine: sfn.StateMachine; | ||
public readonly stateMachineLogGroup: logs.ILogGroup; | ||
public readonly cloudwatchAlarms?: cloudwatch.Alarm[]; | ||
|
||
constructor(scope: Construct, id: string, props: FargateToStepfunctionsProps) { | ||
super(scope, id); | ||
defaults.CheckProps(props); | ||
defaults.CheckFargateProps(props); | ||
|
||
this.vpc = defaults.buildVpc(scope, { | ||
existingVpc: props.existingVpc, | ||
defaultVpcProps: props.publicApi ? defaults.DefaultPublicPrivateVpcProps() : defaults.DefaultIsolatedVpcProps(), | ||
userVpcProps: props.vpcProps, | ||
constructVpcProps: { enableDnsHostnames: true, enableDnsSupport: true } | ||
}); | ||
|
||
defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.STEP_FUNCTIONS); | ||
|
||
if (props.existingFargateServiceObject) { | ||
this.service = props.existingFargateServiceObject; | ||
// CheckFargateProps confirms that the container is provided | ||
this.container = props.existingContainerDefinitionObject!; | ||
} else { | ||
[this.service, this.container] = defaults.CreateFargateService( | ||
scope, | ||
id, | ||
this.vpc, | ||
props.clusterProps, | ||
props.ecrRepositoryArn, | ||
props.ecrImageVersion, | ||
props.fargateTaskDefinitionProps, | ||
props.containerDefinitionProps, | ||
props.fargateServiceProps | ||
); | ||
} | ||
|
||
[this.stateMachine, this.stateMachineLogGroup] = defaults.buildStateMachine(this, props.stateMachineProps, | ||
props.logGroupProps); | ||
|
||
this.stateMachine.grantStartExecution(this.service.taskDefinition.taskRole); | ||
|
||
if (props.createCloudWatchAlarms === undefined || props.createCloudWatchAlarms) { | ||
// Deploy best-practice CloudWatch Alarm for state machine | ||
this.cloudwatchAlarms = defaults.buildStepFunctionCWAlarms(this, this.stateMachine); | ||
} | ||
|
||
// Add environment variable | ||
const stateMachineEnvironmentVariableName = props.stateMachineEnvironmentVariableName || 'STATE_MACHINE_ARN'; | ||
this.container.addEnvironment(stateMachineEnvironmentVariableName, this.stateMachine.stateMachineArn); | ||
} | ||
} |
Oops, something went wrong.