aws / aws/aws-cdk

(aws-lambda): Option to specify VPC to Custom Resource Lambdas

Open
#22,473 4 comments 6 reactions 0 assignees View on GitHub
@aws-cdk/custom-resources effort/small feature-request p1
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

Creating this issue as requested.

Currently, there is no way to specify a VPC for Custom Resource Lambdas. The only solution is to use Aspects like below.

```
import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as lambda from '@aws-cdk/aws-lambda';

interface VpcConfig {
readonly SecurityGroupIds: string[];
readonly SubnetIds: string[];
}

export class LambdaVPCAspect implements cdk.IAspect {
private readonly vpc: ec2.Vpc;

constructor(vpc: ec2.Vpc) {
this.vpc = vpc;
}

public visit(node: cdk.IConstruct): void {
if (node instanceof lambda.CfnFunction) {
if (!node.vpcConfig) {
node.addPropertyOverride('VpcConfig', this.getConfig(node, this.vpc));
}
}
}

private getConfig(node: cdk.Construct, vpc: ec2.Vpc): VpcConfig {
let securityGroups: ec2.ISecurityGroup[];

const securityGroup = new ec2.SecurityGroup(node, 'SecurityGroup', {
vpc: vpc,
});

securityGroups = [securityGroup];
const { subnetIds } = vpc.selectSubnets();

return {
SecurityGroupIds: securityGroups.map(sg => sg.securityGroupId),
SubnetIds: subnetIds,
};
}
}
```

Requesting a feature that allows specifying `VpcId` or similar.

There was a GitHub Issue about this in the past too, linked below. That issue has since been closed.
> https://github.com/aws/aws-cdk/issues/11340

Thank you and please let me know if I can provide any other information.

### Use Case

Need to specify my own custom VPC when creating custom resource Lambdas.

### Proposed Solution

Allow a property like `VpcId` that lets users specify the VPC for custom resource Lambdas.

### Other Information

_No response_

### Acknowledgements

- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

n/a

### Environment details (OS name and version, etc.)

n/a

Contributor guide

Open the contributing guide

Research direction

Start by tracing where the CDK creates Custom Resource Lambdas and review the linked historical issue #11340. Define the supported VPC configuration and its interaction with existing custom-resource functions; done means users can provide a VPC and synthesized Lambda resources receive the corresponding VPC settings, with coverage for the new behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.