|
| 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 | +// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate |
| 16 | +import { Construct } from "@aws-cdk/core"; |
| 17 | +import * as defaults from "@aws-solutions-constructs/core"; |
| 18 | +import * as ecs from "@aws-cdk/aws-ecs"; |
| 19 | +import * as events from "@aws-cdk/aws-events"; |
| 20 | + |
| 21 | +export interface FargateToEventbridgeProps { |
| 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. A Step Functions Interface |
| 34 | + * endpoint will be included in this VPC. |
| 35 | + * |
| 36 | + * @default - A set of defaults from vpc-defaults.ts: DefaultPublicPrivateVpcProps() for public APIs |
| 37 | + * and DefaultIsolatedVpcProps() for private APIs. |
| 38 | + */ |
| 39 | + readonly vpcProps?: ec2.VpcProps; |
| 40 | + /** |
| 41 | + * An existing VPC in which to deploy the construct. Providing both this and |
| 42 | + * vpcProps is an error. If the client provides an existing Fargate service, |
| 43 | + * this value must be the VPC where the service is running. A Step Functions Interface |
| 44 | + * endpoint will be added to this VPC. |
| 45 | + * |
| 46 | + * @default - None |
| 47 | + */ |
| 48 | + readonly existingVpc?: ec2.IVpc; |
| 49 | + /** |
| 50 | + * Optional properties to create a new ECS cluster |
| 51 | + * |
| 52 | + * @default - None |
| 53 | + */ |
| 54 | + readonly clusterProps?: ecs.ClusterProps; |
| 55 | + /** |
| 56 | + * The arn of an ECR Repository containing the image to use |
| 57 | + * to generate the containers |
| 58 | + * |
| 59 | + * format: |
| 60 | + * arn:aws:ecr:[region]:[account number]:repository/[Repository Name] |
| 61 | + * |
| 62 | + * @default - None |
| 63 | + */ |
| 64 | + readonly ecrRepositoryArn?: string; |
| 65 | + /** |
| 66 | + * The version of the image to use from the repository |
| 67 | + * |
| 68 | + * @default - 'latest' |
| 69 | + */ |
| 70 | + readonly ecrImageVersion?: string; |
| 71 | + /* |
| 72 | + * Optional props to define the container created for the Fargate Service |
| 73 | + * |
| 74 | + * @default - fargate-defaults.ts |
| 75 | + */ |
| 76 | + readonly containerDefinitionProps?: ecs.ContainerDefinitionProps | any; |
| 77 | + /* |
| 78 | + * Optional props to define the Fargate Task Definition for this construct |
| 79 | + * |
| 80 | + * @default - fargate-defaults.ts |
| 81 | + */ |
| 82 | + readonly fargateTaskDefinitionProps?: ecs.FargateTaskDefinitionProps | any; |
| 83 | + /** |
| 84 | + * Optional values to override default Fargate Task definition properties |
| 85 | + * (fargate-defaults.ts). The construct will default to launching the service |
| 86 | + * is the most isolated subnets available (precedence: Isolated, Private and |
| 87 | + * Public). Override those and other defaults here. |
| 88 | + * |
| 89 | + * @default - fargate-defaults.ts |
| 90 | + */ |
| 91 | + readonly fargateServiceProps?: ecs.FargateServiceProps | any; |
| 92 | + /** |
| 93 | + * A Fargate Service already instantiated (probably by another Solutions Construct). If |
| 94 | + * this is specified, then no props defining a new service can be provided, including: |
| 95 | + * existingImageObject, ecrImageVersion, containerDefintionProps, fargateTaskDefinitionProps, |
| 96 | + * ecrRepositoryArn, fargateServiceProps, clusterProps, existingClusterInterface. If this value |
| 97 | + * is provided, then existingContainerDefinitionObject must be provided as well. |
| 98 | + * |
| 99 | + * @default - None |
| 100 | + */ |
| 101 | + readonly existingFargateServiceObject?: ecs.FargateService; |
| 102 | + /* |
| 103 | + * A container definition already instantiated as part of a Fargate service. This must |
| 104 | + * be the container in the existingFargateServiceObject. |
| 105 | + * |
| 106 | + * @default - None |
| 107 | + */ |
| 108 | + readonly existingContainerDefinitionObject?: ecs.ContainerDefinition; |
| 109 | + /** |
| 110 | + * Existing instance of a custom EventBus. |
| 111 | + * |
| 112 | + * @default - None |
| 113 | + */ |
| 114 | + readonly existingEventBusInterface?: events.IEventBus; |
| 115 | + /** |
| 116 | + * A new custom EventBus is created with provided props. |
| 117 | + * |
| 118 | + * @default - None |
| 119 | + */ |
| 120 | + readonly eventBusProps?: events.EventBusProps; |
| 121 | + /** |
| 122 | + * Optional Name for the container environment variable set to the DynamoDB table name. |
| 123 | + * |
| 124 | + * @default - EVENTBUS_NAME |
| 125 | + */ |
| 126 | + readonly eventBusEnvironmentVariableName?: string; |
| 127 | +} |
| 128 | + |
| 129 | +export class FargateToEventbridge extends Construct { |
| 130 | + public readonly vpc: ec2.IVpc; |
| 131 | + public readonly service: ecs.FargateService; |
| 132 | + public readonly container: ecs.ContainerDefinition; |
| 133 | + public readonly eventBus?: events.IEventBus; |
| 134 | + |
| 135 | + constructor(scope: Construct, id: string, props: FargateToEventbridgeProps) { |
| 136 | + super(scope, id); |
| 137 | + defaults.CheckProps(props); |
| 138 | + defaults.CheckFargateProps(props); |
| 139 | + |
| 140 | + this.vpc = defaults.buildVpc(scope, { |
| 141 | + existingVpc: props.existingVpc, |
| 142 | + defaultVpcProps: props.publicApi ? defaults.DefaultPublicPrivateVpcProps() : defaults.DefaultIsolatedVpcProps(), |
| 143 | + userVpcProps: props.vpcProps, |
| 144 | + constructVpcProps: { enableDnsHostnames: true, enableDnsSupport: true } |
| 145 | + }); |
| 146 | + |
| 147 | + defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.EVENTS); |
| 148 | + |
| 149 | + if (props.existingFargateServiceObject) { |
| 150 | + this.service = props.existingFargateServiceObject; |
| 151 | + // CheckFargateProps confirms that the container is provided |
| 152 | + this.container = props.existingContainerDefinitionObject!; |
| 153 | + } else { |
| 154 | + [this.service, this.container] = defaults.CreateFargateService( |
| 155 | + scope, |
| 156 | + id, |
| 157 | + this.vpc, |
| 158 | + props.clusterProps, |
| 159 | + props.ecrRepositoryArn, |
| 160 | + props.ecrImageVersion, |
| 161 | + props.fargateTaskDefinitionProps, |
| 162 | + props.containerDefinitionProps, |
| 163 | + props.fargateServiceProps |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + this.eventBus = defaults.buildEventBus(this, { |
| 168 | + existingEventBusInterface: props.existingEventBusInterface, |
| 169 | + eventBusProps: props.eventBusProps |
| 170 | + }); |
| 171 | + |
| 172 | + if (this.eventBus) { |
| 173 | + this.eventBus.grantPutEventsTo(this.service.taskDefinition.taskRole); |
| 174 | + } else { |
| 175 | + // Since user didn't specify custom event bus, provide permissions on default event bus |
| 176 | + const defaultEventBus = events.EventBus.fromEventBusName(this, 'default-event-bus', 'default'); |
| 177 | + defaultEventBus.grantPutEventsTo(this.service.taskDefinition.taskRole); |
| 178 | + } |
| 179 | + |
| 180 | + // Add environment variables |
| 181 | + const eventBusEnvironmentVariableName = props.eventBusEnvironmentVariableName || 'EVENTBUS_NAME'; |
| 182 | + const eventBusName = this.eventBus?.eventBusName || 'default'; |
| 183 | + this.container.addEnvironment(eventBusEnvironmentVariableName, eventBusName); |
| 184 | + } |
| 185 | +} |
0 commit comments