Not Error out on AWS Lambda VPC config if both subnet_ids and security_group_ids are empty. (#6191)

AWS Lambda VPC config is an optional configuration and which needs to both subnet_ids and
security_group_ids to tie the lambda function to a VPC. We should make it optional if
both subnet_ids and security_group_ids are not net which would add better flexiblity in
creation of more useful modules as there are "if else" checks. Without this we are creating
duplicate modules one with VPC and one without VPC resulting in various anomalies.
This commit is contained in:
Srikalyan Swayampakula 2016-08-15 22:22:42 +05:30 committed by Radek Simko
parent 1af3e4fc0c
commit 2aa28c34ca
2 changed files with 20 additions and 11 deletions

View File

@ -181,19 +181,21 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
return err
}
var subnetIds []*string
for _, id := range config["subnet_ids"].(*schema.Set).List() {
subnetIds = append(subnetIds, aws.String(id.(string)))
}
if config != nil {
var subnetIds []*string
for _, id := range config["subnet_ids"].(*schema.Set).List() {
subnetIds = append(subnetIds, aws.String(id.(string)))
}
var securityGroupIds []*string
for _, id := range config["security_group_ids"].(*schema.Set).List() {
securityGroupIds = append(securityGroupIds, aws.String(id.(string)))
}
var securityGroupIds []*string
for _, id := range config["security_group_ids"].(*schema.Set).List() {
securityGroupIds = append(securityGroupIds, aws.String(id.(string)))
}
params.VpcConfig = &lambda.VpcConfig{
SubnetIds: subnetIds,
SecurityGroupIds: securityGroupIds,
params.VpcConfig = &lambda.VpcConfig{
SubnetIds: subnetIds,
SecurityGroupIds: securityGroupIds,
}
}
}
@ -402,6 +404,11 @@ func validateVPCConfig(v interface{}) (map[string]interface{}, error) {
return nil, errors.New("vpc_config is <nil>")
}
// if subnet_ids and security_group_ids are both empty then the VPC is optional
if config["subnet_ids"].(*schema.Set).Len() == 0 && config["security_group_ids"].(*schema.Set).Len() == 0 {
return nil, nil
}
if config["subnet_ids"].(*schema.Set).Len() == 0 {
return nil, errors.New("vpc_config.subnet_ids cannot be empty")
}

View File

@ -65,6 +65,8 @@ resource "aws_lambda_function" "test_lambda" {
* `subnet_ids` - (Required) A list of subnet IDs associated with the Lambda function.
* `security_group_ids` - (Required) A list of security group IDs associated with the Lambda function.
~> **NOTE:** if both `subnet_ids` and `security_group_ids` are empty then vpc_config is considered to be empty or unset.
## Attributes Reference
* `arn` - The Amazon Resource Name (ARN) identifying your Lambda Function.