-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apprunner): VpcConnector construct (#20471)
closes #18887 - Creates a new VpcConnector construct. - Make the app runner service construct accept a `vpcConnector` property. When present, associate the service with the connector. ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
28 changed files
with
2,180 additions
and
23 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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// AWS::AppRunner CloudFormation Resources: | ||
export * from './apprunner.generated'; | ||
export * from './service'; | ||
export * from './vpc-connector'; |
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
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,154 @@ | ||
import * as ec2 from '@aws-cdk/aws-ec2'; | ||
import { Connections } from '@aws-cdk/aws-ec2'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { CfnVpcConnector } from './apprunner.generated'; | ||
|
||
/** | ||
* Properties of the AppRunner VPC Connector | ||
*/ | ||
export interface VpcConnectorProps { | ||
/** | ||
* The VPC for the VPC Connector. | ||
*/ | ||
readonly vpc: ec2.IVpc; | ||
|
||
/** | ||
* Where to place the VPC Connector within the VPC. | ||
* | ||
* @default - Private subnets. | ||
*/ | ||
readonly vpcSubnets?: ec2.SubnetSelection; | ||
|
||
/** | ||
* A list of IDs of security groups that App Runner should use for access to AWS resources under the specified subnets. | ||
* | ||
* @default - a new security group will be created in the specified VPC | ||
*/ | ||
readonly securityGroups?: ec2.ISecurityGroup[]; | ||
|
||
/** | ||
* The name for the VpcConnector. | ||
* | ||
* @default - a name generated by CloudFormation | ||
*/ | ||
readonly vpcConnectorName?: string; | ||
} | ||
|
||
/** | ||
* Attributes for the App Runner VPC Connector | ||
*/ | ||
export interface VpcConnectorAttributes { | ||
/** | ||
* The name of the VPC connector. | ||
*/ | ||
readonly vpcConnectorName: string; | ||
|
||
/** | ||
* The ARN of the VPC connector. | ||
*/ | ||
readonly vpcConnectorArn: string; | ||
|
||
/** | ||
* The revision of the VPC connector. | ||
*/ | ||
readonly vpcConnectorRevision: number; | ||
|
||
/** | ||
* The security groups associated with the VPC connector. | ||
*/ | ||
readonly securityGroups: ec2.ISecurityGroup[]; | ||
} | ||
|
||
/** | ||
* Represents the App Runner VPC Connector. | ||
*/ | ||
export interface IVpcConnector extends cdk.IResource, ec2.IConnectable { | ||
/** | ||
* The Name of the VPC connector. | ||
* @attribute | ||
*/ | ||
readonly vpcConnectorName: string; | ||
|
||
/** | ||
* The ARN of the VPC connector. | ||
* @attribute | ||
*/ | ||
readonly vpcConnectorArn: string; | ||
|
||
/** | ||
* The revision of the VPC connector. | ||
* @attribute | ||
*/ | ||
readonly vpcConnectorRevision: number; | ||
} | ||
|
||
/** | ||
* The App Runner VPC Connector | ||
* | ||
* @resource AWS::AppRunner::VpcConnector | ||
*/ | ||
export class VpcConnector extends cdk.Resource implements IVpcConnector { | ||
/** | ||
* Import from VPC connector attributes. | ||
*/ | ||
public static fromVpcConnectorAttributes(scope: Construct, id: string, attrs: VpcConnectorAttributes): IVpcConnector { | ||
const vpcConnectorArn = attrs.vpcConnectorArn; | ||
const vpcConnectorName = attrs.vpcConnectorName; | ||
const vpcConnectorRevision = attrs.vpcConnectorRevision; | ||
const securityGroups = attrs.securityGroups; | ||
|
||
class Import extends cdk.Resource { | ||
public readonly vpcConnectorArn = vpcConnectorArn | ||
public readonly vpcConnectorName = vpcConnectorName | ||
public readonly vpcConnectorRevision = vpcConnectorRevision | ||
public readonly connections = new Connections({ securityGroups }); | ||
} | ||
|
||
return new Import(scope, id); | ||
} | ||
|
||
/** | ||
* The ARN of the VPC connector. | ||
* @attribute | ||
*/ | ||
readonly vpcConnectorArn: string; | ||
|
||
/** | ||
* The revision of the VPC connector. | ||
* @attribute | ||
*/ | ||
readonly vpcConnectorRevision: number; | ||
|
||
/** | ||
* The name of the VPC connector. | ||
* @attribute | ||
*/ | ||
readonly vpcConnectorName: string; | ||
|
||
/** | ||
* Allows specifying security group connections for the VPC connector. | ||
*/ | ||
public readonly connections: Connections | ||
|
||
public constructor(scope: Construct, id: string, props: VpcConnectorProps) { | ||
super(scope, id, { | ||
physicalName: props.vpcConnectorName, | ||
}); | ||
|
||
const securityGroups = props.securityGroups?.length ? | ||
props.securityGroups | ||
: [new ec2.SecurityGroup(this, 'SecurityGroup', { vpc: props.vpc })]; | ||
|
||
const resource = new CfnVpcConnector(this, 'Resource', { | ||
subnets: props.vpc.selectSubnets(props.vpcSubnets).subnetIds, | ||
securityGroups: cdk.Lazy.list({ produce: () => this.connections.securityGroups.map(sg => sg.securityGroupId) }), | ||
vpcConnectorName: this.physicalName, | ||
}); | ||
|
||
this.vpcConnectorArn = resource.attrVpcConnectorArn; | ||
this.vpcConnectorRevision = resource.attrVpcConnectorRevision; | ||
this.vpcConnectorName = resource.ref; | ||
this.connections = new Connections({ securityGroups }); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
packages/@aws-cdk/aws-apprunner/test/integ.service-vpc-connector.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,51 @@ | ||
import * as ec2 from '@aws-cdk/aws-ec2'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { Service, Source, VpcConnector } from '../lib'; | ||
|
||
|
||
const app = new cdk.App(); | ||
|
||
const stack = new cdk.Stack(app, 'integ-apprunner'); | ||
|
||
// Scenario 6: Create the service from ECR public with a VPC Connector | ||
const vpc = new ec2.Vpc(stack, 'Vpc', { | ||
cidr: '10.0.0.0/16', | ||
}); | ||
|
||
const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup', { vpc }); | ||
|
||
const vpcConnector = new VpcConnector(stack, 'VpcConnector', { | ||
vpc, | ||
vpcSubnets: vpc.selectSubnets({ subnetType: ec2.SubnetType.PUBLIC }), | ||
securityGroups: [securityGroup], | ||
vpcConnectorName: 'MyVpcConnector', | ||
}); | ||
|
||
const service6 = new Service(stack, 'Service6', { | ||
source: Source.fromEcrPublic({ | ||
imageConfiguration: { | ||
port: 8000, | ||
}, | ||
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', | ||
}), | ||
vpcConnector, | ||
}); | ||
new cdk.CfnOutput(stack, 'URL6', { value: `https://${service6.serviceUrl}` }); | ||
|
||
// Scenario 7: Create the service from ECR public and associate it with an existing VPC Connector | ||
|
||
const service7 = new Service(stack, 'Service7', { | ||
source: Source.fromEcrPublic({ | ||
imageConfiguration: { | ||
port: 8000, | ||
}, | ||
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', | ||
}), | ||
vpcConnector: VpcConnector.fromVpcConnectorAttributes(stack, 'ImportedVpcConnector', { | ||
vpcConnectorArn: vpcConnector.vpcConnectorArn, | ||
vpcConnectorName: vpcConnector.vpcConnectorName, | ||
vpcConnectorRevision: vpcConnector.vpcConnectorRevision, | ||
securityGroups: [securityGroup], | ||
}), | ||
}); | ||
new cdk.CfnOutput(stack, 'URL7', { value: `https://${service7.serviceUrl}` }); |
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/aws-apprunner/test/service-ecr-public.integ.snapshot/cdk.out
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"17.0.0"} | ||
{"version":"19.0.0"} |
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: 2 additions & 2 deletions
4
packages/@aws-cdk/aws-apprunner/test/service-ecr-public.integ.snapshot/integ.json
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
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/aws-apprunner/test/service-ecr-public.integ.snapshot/manifest.json
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "17.0.0", | ||
"version": "19.0.0", | ||
"artifacts": { | ||
"Tree": { | ||
"type": "cdk:tree", | ||
|
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
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/aws-apprunner/test/service-ecr.integ.snapshot/cdk.out
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"17.0.0"} | ||
{"version":"19.0.0"} |
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: 2 additions & 2 deletions
4
packages/@aws-cdk/aws-apprunner/test/service-ecr.integ.snapshot/integ.json
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
Oops, something went wrong.