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

Allow to deploy Lambda inside VPC #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions config/sidecar.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@
* See CreateExecutionRole::policy for the IAM policy.
*/
'execution_role' => env('SIDECAR_EXECUTION_ROLE'),

/*
* This is the VPC Configuration for Lambda. VPCs are optional
* and may be left empty if there are no networking needs
* to place sidecar lambda functions inside a VPC.
*/
'vpc' => [
'security_groups' => env('SIDECAR_VPC_SECURITY_GROUP'),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'security_groups' => env('SIDECAR_VPC_SECURITY_GROUP'),
'security_groups' => explode(',', env('SIDECAR_VPC_SECURITY_GROUPS', '')),

'subnets' => env('SIDECAR_VPC_SUBNETS'),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'subnets' => env('SIDECAR_VPC_SUBNETS'),
'subnets' => explode(',', env('SIDECAR_VPC_SUBNETS', '')),

]
];
33 changes: 32 additions & 1 deletion src/LambdaFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,29 @@ public function timeout()
return config('sidecar.timeout');
}

/**
* Lambda Function VPC Configuration. This option is often used to place
* functions within a VPC for accessibility to private RDS or
* Elasticache instances that are not publicly accessible.
*
* @return null|array
*/
public function vpc()
{
$subnets = config('sidecar.vpc.subnets');

$sg = config('sidecar.vpc.security_group');
Copy link
Contributor

Choose a reason for hiding this comment

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

The AWS config is SecurityGroupIds, it should be plural and an array.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$sg = config('sidecar.vpc.security_group');
$securityGroups = config('sidecar.vpc.security_groups');


if ($subnets && $sg) {
return [
'SecurityGroupIds' => Arr::wrap($sg),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'SecurityGroupIds' => Arr::wrap($sg),
'SecurityGroupIds' => $securityGroups,

'SubnetIds' => Arr::wrap($subnets),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'SubnetIds' => Arr::wrap($subnets),
'SubnetIds' => $subnets,

];
}

return null;
}

public function preparePayload($payload)
{
return $payload;
Expand Down Expand Up @@ -395,7 +418,8 @@ public function toDeploymentArray()
'Layers' => $this->layers(),
'Publish' => true,
'PackageType' => $this->packageType(),
'Architectures' => [$this->architecture()]
'Architectures' => [$this->architecture()],
'VpcConfig' => $this->vpc(),
];

// For container image packages, we need to remove the Runtime
Expand All @@ -405,6 +429,13 @@ public function toDeploymentArray()
$config = Arr::except($config, ['Runtime', 'Handler']);
}

// Vpc Configuration is optional so let's delete it
// from the final configuration if the user did
// not opt to define a VPC.
if ($config['VpcConfig'] === null) {
unset($config['VpcConfig']);
}

return $config;
}
}
33 changes: 33 additions & 0 deletions tests/Unit/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,37 @@ public function memory_and_timeout_get_cast_to_ints()
$this->assertSame(5, $array['Timeout']);
$this->assertSame(500, $array['MemorySize']);
}

/** @test */
public function test_lambda_function_inside_vpc()
{
config([
'sidecar.vpc' => [
'security_group' => ['sg-12345678'],
'subnets' => ['subnet-e000ab00'],
],
]);

$array = (new EmptyTestFunction)->toDeploymentArray();

$this->assertSame('sg-12345678', $array['VpcConfig']['SecurityGroupIds'][0]);
$this->assertSame('subnet-e000ab00', $array['VpcConfig']['SubnetIds'][0]);
}

/** @test */
public function test_let_user_define_single_subnet_and_sg()
{
config([
'sidecar.vpc' => [
'security_group' => 'sg-12345678',
'subnets' => 'subnet-e000ab00',
],
]);

$array = (new EmptyTestFunction)->toDeploymentArray();

$this->assertSame('sg-12345678', $array['VpcConfig']['SecurityGroupIds'][0]);
$this->assertSame('subnet-e000ab00', $array['VpcConfig']['SubnetIds'][0]);
}

}