Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apprunner): support vpc ingress connection #30623

Merged
merged 14 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-apprunner-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,42 @@ new apprunner.Service(this, 'Service', {
});
```

## VPC Ingress Connection

You need to create a VPC Ingress Connection to make your App Runner service private and only accessible from within a VPC.

To set up a VPC Ingress Connection, you need to associate a VPC, a VPC Interface Endpoint, and an App Runner Service.
mazyu36 marked this conversation as resolved.
Show resolved Hide resolved

For more information, see [Enabling Private endpoint for incoming traffic](https://docs.aws.amazon.com/apprunner/latest/dg/network-pl.html).

```typescript
import * as ec2 from 'aws-cdk-lib/aws-ec2';

declare const vpc: ec2.Vpc;

const interfaceVpcEndpoint = new ec2.InterfaceVpcEndpoint(this, 'MyVpcEndpoint', {
vpc,
service: ec2.InterfaceVpcEndpointAwsService.APP_RUNNER_REQUESTS,
privateDnsEnabled: false,
});

const service = new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: {
port: 8000,
},
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
isPubliclyAccessible: false,
});

new apprunner.VpcIngressConnection(this, 'VpcIngressConnection', {
vpc,
interfaceVpcEndpoint,
service,
});
```

## Dual Stack

To use dual stack (IPv4 and IPv6) for your incoming public network configuration, set `ipAddressType` to `IpAddressType.DUAL_STACK`.
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-apprunner-alpha/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './auto-scaling-configuration';
export * from './observability-configuration';
export * from './service';
export * from './vpc-connector';
export * from './vpc-ingress-connection';
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,13 @@ export interface ServiceProps {
*/
readonly vpcConnector?: IVpcConnector;

/**
* Specifies whether your App Runner service is publicly accessible.
*
* @default true
*/
readonly isPubliclyAccessible?: boolean;
paulhcsun marked this conversation as resolved.
Show resolved Hide resolved

/**
* Settings for the health check that AWS App Runner performs to monitor the health of a service.
*
Expand Down Expand Up @@ -1300,6 +1307,7 @@ export class Service extends cdk.Resource implements iam.IGrantable {
egressType: this.props.vpcConnector ? 'VPC' : 'DEFAULT',
vpcConnectorArn: this.props.vpcConnector?.vpcConnectorArn,
},
ingressConfiguration: props.isPubliclyAccessible !== undefined ? { isPubliclyAccessible: props.isPubliclyAccessible } : undefined,
ipAddressType: this.props.ipAddressType,
},
healthCheckConfiguration: this.props.healthCheck ?
Expand Down
168 changes: 168 additions & 0 deletions packages/@aws-cdk/aws-apprunner-alpha/lib/vpc-ingress-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as cdk from 'aws-cdk-lib/core';
import { Construct } from 'constructs';
import { IService } from './service';
import { CfnVpcIngressConnection } from 'aws-cdk-lib/aws-apprunner';

/**
* Properties of the AppRunner VPC Ingress Connection
*/
export interface VpcIngressConnectionProps {
/**
* The name for the VPC Ingress Connection.
*
* @default - a name generated by CloudFormation
*/
readonly vpcIngressConnectionName?: string;

/**
* The service to connect.
*/
readonly service: IService;

/**
* The VPC for the VPC Ingress Connection.
*/
readonly vpc: ec2.IVpc;

/**
* The VPC Interface Endpoint for the VPC Ingress Connection.
*/
readonly interfaceVpcEndpoint: ec2.IInterfaceVpcEndpoint;
}

/**
* Attributes for the App Runner VPC Ingress Connection
*/
export interface VpcIngressConnectionAttributes {
/**
* The Amazon Resource Name (ARN) of the VPC Ingress Connection.
*/
readonly vpcIngressConnectionArn: string;

/**
* The name of the VPC Ingress Connection.
*/
readonly vpcIngressConnectionName: string;

/**
* The domain name associated with the VPC Ingress Connection resource.
*/
readonly domainName: string;

/**
* The current status of the VPC Ingress Connection.
*/
readonly status: string;
paulhcsun marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Represents the App Runner VPC Ingress Connection.
*/
export interface IVpcIngressConnection extends cdk.IResource {
/**
* The Amazon Resource Name (ARN) of the VPC Ingress Connection.
* @attribute
*/
readonly vpcIngressConnectionArn: string;

/**
* The name of the VPC Ingress Connection.
* @attribute
*/
readonly vpcIngressConnectionName: string;
}

/**
* The App Runner VPC Ingress Connection
*
* @resource AWS::AppRunner::VpcIngressConnection
*/
export class VpcIngressConnection extends cdk.Resource implements IVpcIngressConnection {
/**
* Import from VPC Ingress Connection from attributes.
*/
public static fromVpcIngressConnectionAttributes(scope: Construct, id: string, attrs: VpcIngressConnectionAttributes): IVpcIngressConnection {
const vpcIngressConnectionArn = attrs.vpcIngressConnectionArn;
const domainName = attrs.domainName;
const status = attrs.status;
const vpcIngressConnectionName = attrs.vpcIngressConnectionName;

class Import extends cdk.Resource implements IVpcIngressConnection {
public readonly vpcIngressConnectionArn = vpcIngressConnectionArn;
public readonly domainName = domainName;
public readonly status = status;
public readonly vpcIngressConnectionName = vpcIngressConnectionName;
}

return new Import(scope, id);
}

/**
* Imports an App Runner VPC Ingress Connection from its ARN
*/
public static fromArn(scope: Construct, id: string, vpcIngressConnectionArn: string): IVpcIngressConnection {
const resourceParts = cdk.Fn.split('/', vpcIngressConnectionArn);

const vpcIngressConnectionName = cdk.Fn.select(0, resourceParts);

class Import extends cdk.Resource implements IVpcIngressConnection {
public readonly vpcIngressConnectionName = vpcIngressConnectionName;
public readonly vpcIngressConnectionArn = vpcIngressConnectionArn;
}

return new Import(scope, id);
}

/**
* The ARN of the VPC Ingress Connection.
* @attribute
*/
readonly vpcIngressConnectionArn: string;

/**
* The domain name associated with the VPC Ingress Connection resource.
* @attribute
*/
readonly domainName: string;

/**
* The current status of the VPC Ingress Connection.
* @attribute
*/
readonly status: string;

/**
* The name of the VPC Ingress Connection.
* @attribute
*/
readonly vpcIngressConnectionName: string;

public constructor(scope: Construct, id: string, props: VpcIngressConnectionProps) {
super(scope, id, {
physicalName: props.vpcIngressConnectionName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check if it deploys with vpcIngressConnectionName: undefined?
If not, we should lazily generate one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I tried deploying with vpcIngressConnectionName: undefined, and I was able to deploy without any issues.

image image

});

if (
props.vpcIngressConnectionName !== undefined &&
!cdk.Token.isUnresolved(props.vpcIngressConnectionName) &&
!/^[A-Za-z0-9][A-Za-z0-9\-_]{3,39}$/.test(props.vpcIngressConnectionName)
paulhcsun marked this conversation as resolved.
Show resolved Hide resolved
) {
throw new Error(`vpcIngressConnectionName must match the \`^[A-Za-z0-9][A-Za-z0-9\-_]{3,39}\` pattern, got ${props.vpcIngressConnectionName}`);
}

const resource = new CfnVpcIngressConnection(this, 'Resource', {
ingressVpcConfiguration: {
vpcEndpointId: props.interfaceVpcEndpoint.vpcEndpointId,
vpcId: props.vpc.vpcId,
},
serviceArn: props.service.serviceArn,
vpcIngressConnectionName: this.physicalName,
});

this.vpcIngressConnectionArn = resource.attrVpcIngressConnectionArn;
this.vpcIngressConnectionName = resource.ref;
this.domainName = resource.attrDomainName;
this.status = resource.attrStatus;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading