Skip to content

Commit

Permalink
feat(s3-deployment): support securityGroups in BucketDeploymentProps
Browse files Browse the repository at this point in the history
  • Loading branch information
drduhe committed Jan 30, 2025
1 parent 6ea230c commit ba8b378
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/aws-cdk-lib/aws-s3-deployment/lib/bucket-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ export interface BucketDeploymentProps {
* @default true
*/
readonly outputObjectKeys?: boolean;

/**
* The list of security groups to associate with the lambda handlers network interfaces.
*
* Only used if 'vpc' is supplied.
*
* @default undefined - If the function is placed within a VPC and a security group is
* not specified, either by this or securityGroup prop, a dedicated security
* group will be created for this function.
*/
readonly securityGroups?: ec2.ISecurityGroup[];
}

/**
Expand Down Expand Up @@ -366,6 +377,7 @@ export class BucketDeployment extends Construct {
ephemeralStorageSize: props.ephemeralStorageSize,
vpc: props.vpc,
vpcSubnets: props.vpcSubnets,
securityGroups: props.securityGroups ? props.securityGroups : undefined,
filesystem: accessPoint ? lambda.FileSystem.fromEfsAccessPoint(
accessPoint,
mountPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,36 @@ test('deployment allows vpc and subnets to be implicitly supplied to lambda', ()
});
});

test('deployment allows security groups to be explicitly supplied to lambda', () => {
// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Dest');
const vpc = new ec2.Vpc(stack, 'SomeVpc', {});
const securityGroup = new ec2.SecurityGroup(stack, 'SomeSecurityGroup', { vpc });

// WHEN
new s3deploy.BucketDeployment(stack, 'DeployWithVpc1', {
sources: [s3deploy.Source.asset(path.join(__dirname, 'my-website'))],
destinationBucket: bucket,
vpc,
securityGroups: [securityGroup],
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
VpcConfig: Match.objectLike({
SecurityGroupIds: Match.arrayWith([
{
'Fn::GetAtt': Match.arrayWith([
Match.stringLikeRegexp('SomeSecurityGroup'), // Matches dynamically generated SG name
'GroupId',
]),
},
]),
}),
});
});

test('s3 deployment bucket is identical to destination bucket', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit ba8b378

Please sign in to comment.