Skip to content

Commit 316f966

Browse files
authored
Merge pull request #17 from Martijn02/feature/ipv6-allowed-for-dual-stack
feat: Add AWS lambda support for IPV6 outbound connections in VPC
2 parents e53b84a + ee0326e commit 316f966

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

docs/guides/functions.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,51 @@ The Lambda function execution role must have permissions to create, describe and
529529
By default, when a Lambda function is executed inside a VPC, it loses internet access and some resources inside AWS may become unavailable. In order for S3 resources and DynamoDB resources to be available for your Lambda function running inside the VPC, a VPC end point needs to be created. For more information please check [VPC Endpoint for Amazon S3](https://aws.amazon.com/blogs/aws/new-vpc-endpoint-for-amazon-s3/).
530530
In order for other services such as Kinesis streams to be made available, a NAT Gateway needs to be configured inside the subnets that are being used to run the Lambda, for the VPC used to execute the Lambda. For more information, please check [Enable Outgoing Internet Access within VPC](https://medium.com/@philippholly/aws-lambda-enable-outgoing-internet-access-within-vpc-8dd250e11e12)
531531

532+
**VPC Lambda Internet IPv6 Access**
533+
534+
Alternatively to setting up a NAT Gateway, you can also use an [egress-only internet gateway](https://docs.aws.amazon.com/vpc/latest/userguide/egress-only-internet-gateway.html) and allow your functions in a VPC to access the internet or other AWS services via IPv6. This eliminates the need for a NAT Gateway, reducing costs and simplifying architecture. In this case, VPC-configured Lambda functions can be allowed to access the internet using egress-only internet gateway by adding a `ipv6AllowedForDualStack` option to either the functions VPC specification:
535+
536+
```yml
537+
# serverless.yml
538+
service: service-name
539+
provider: aws
540+
541+
functions:
542+
hello:
543+
handler: handler.hello
544+
vpc:
545+
ipv6AllowedForDualStack: true
546+
securityGroupIds:
547+
- securityGroupId1
548+
- securityGroupId2
549+
subnetIds:
550+
- subnetId1
551+
- subnetId2
552+
```
553+
554+
Or if you want to apply VPC configuration to all functions in your service, you can add the configuration to the higher level `provider` object, and overwrite these service level config at the function level. For example:
555+
556+
```yml
557+
# serverless.yml
558+
service: service-name
559+
provider:
560+
name: aws
561+
vpc:
562+
ipv6AllowedForDualStack: true
563+
securityGroupIds:
564+
- securityGroupId1
565+
- securityGroupId2
566+
subnetIds:
567+
- subnetId1
568+
- subnetId2
569+
570+
functions:
571+
...
572+
```
573+
574+
For more information, please check [Announcing AWS Lambda’s support for Internet Protocol Version 6 (IPv6) for outbound connections in VPC](https://aws.amazon.com/about-aws/whats-new/2023/10/aws-lambda-ipv6-outbound-connections-vpc/)
575+
576+
532577
## Environment Variables
533578

534579
You can add environment variable configuration to a specific function in `serverless.yml` by adding an `environment` object property in the function configuration. This object should contain a key-value pairs of string to string:

docs/guides/serverless.yml.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,9 @@ Configure the Lambda functions to run inside a VPC ([complete documentation](./f
490490
```yml
491491
provider:
492492
# Optional VPC settings
493-
# If you use VPC then both securityGroupIds and subnetIds are required
493+
# If you use VPC then both securityGroupIds and subnetIds are required, ipv6AllowedForDualStack is optional
494494
vpc:
495+
ipv6AllowedForDualStack: true
495496
securityGroupIds:
496497
- securityGroupId1
497498
- securityGroupId2
@@ -647,6 +648,7 @@ functions:
647648
# If you use VPC then both subproperties (securityGroupIds and subnetIds) are required
648649
# Can be set to '~' to disable the use of a VPC
649650
vpc:
651+
ipv6AllowedForDualStack: true
650652
securityGroupIds:
651653
- securityGroupId1
652654
- securityGroupId2

lib/plugins/aws/deploy-function.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ class AwsDeployFunction {
378378
const vpc = functionObj.vpc || providerObj.vpc;
379379
params.VpcConfig = {};
380380

381+
if (vpc.ipv6AllowedForDualStack) {
382+
params.VpcConfig.Ipv6AllowedForDualStack = vpc.ipv6AllowedForDualStack;
383+
}
384+
381385
if (Array.isArray(vpc.securityGroupIds) && !vpc.securityGroupIds.some(_.isObject)) {
382386
params.VpcConfig.SecurityGroupIds = vpc.securityGroupIds;
383387
}
@@ -387,8 +391,11 @@ class AwsDeployFunction {
387391
}
388392

389393
const didVpcChange = () => {
390-
const remoteConfigToCompare = { SecurityGroupIds: [], SubnetIds: [] };
394+
const remoteConfigToCompare = { Ipv6AllowedForDualStack: false, SecurityGroupIds: [], SubnetIds: [] };
391395
if (remoteFunctionConfiguration.VpcConfig) {
396+
remoteConfigToCompare.Ipv6AllowedForDualStack = new Set(
397+
remoteFunctionConfiguration.VpcConfig.Ipv6AllowedForDualStack || false
398+
);
392399
remoteConfigToCompare.SecurityGroupIds = new Set(
393400
remoteFunctionConfiguration.VpcConfig.SecurityGroupIds || []
394401
);
@@ -397,6 +404,7 @@ class AwsDeployFunction {
397404
);
398405
}
399406
const localConfigToCompare = {
407+
Ipv6AllowedForDualStack: new Set(params.VpcConfig.Ipv6AllowedForDualStack || false),
400408
SecurityGroupIds: new Set(params.VpcConfig.SecurityGroupIds || []),
401409
SubnetIds: new Set(params.VpcConfig.SubnetIds || []),
402410
};

lib/plugins/aws/package/compile/functions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ class AwsCompileFunctions {
389389
if (!this.serverless.service.provider.vpc) this.serverless.service.provider.vpc = {};
390390

391391
functionResource.Properties.VpcConfig = {
392+
Ipv6AllowedForDualStack:
393+
functionObject.vpc.ipv6AllowedForDualStack ||
394+
this.serverless.service.provider.vpc.ipv6AllowedForDualStack,
392395
SecurityGroupIds:
393396
functionObject.vpc.securityGroupIds ||
394397
this.serverless.service.provider.vpc.securityGroupIds,

lib/plugins/aws/provider.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ class AwsProvider {
661661
awsLambdaVpcConfig: {
662662
type: 'object',
663663
properties: {
664+
ipv6AllowedForDualStack: { type: 'boolean' },
664665
securityGroupIds: {
665666
anyOf: [
666667
{

0 commit comments

Comments
 (0)