@@ -2,126 +2,115 @@ import * as cdk from 'aws-cdk-lib';
22import { Construct } from 'constructs' ;
33
44export interface LambdaElasticIpStackProps extends cdk . StackProps {
5- availabilityZone ?: string ;
6- cidrBlock ?: string ;
7- routeTableId ?: string ;
5+ vpcId ?: string ;
6+ subnetId ?: string ;
7+ securityGroupId ?: string ;
88}
99
1010interface AssociateLambdaToElasticIpCRProps {
11- elasticIP : cdk . aws_ec2 . CfnEIP ;
12- vpc : cdk . aws_ec2 . IVpc ;
13- publicSubnet : cdk . aws_ec2 . Subnet ;
14- securityGroup : cdk . aws_ec2 . SecurityGroup ;
15- functionName : string ;
11+ elasticIP : cdk . aws_ec2 . CfnEIP ;
12+ vpc : cdk . aws_ec2 . IVpc ;
13+ publicSubnet : cdk . aws_ec2 . ISubnet ;
14+ securityGroup : cdk . aws_ec2 . ISecurityGroup ;
15+ functionName : string ;
1616}
1717export class LambdaElasticIpStack extends cdk . Stack {
18- constructor ( scope : Construct , id : string , props : LambdaElasticIpStackProps ) {
19- super ( scope , id , props ) ;
18+ constructor ( scope : Construct , id : string , props : LambdaElasticIpStackProps ) {
19+ super ( scope , id , props ) ;
2020
21- const vpc = cdk . aws_ec2 . Vpc . fromLookup ( this , 'Default-VPC' , { isDefault : true } ) ;
22- const publicSubnet = new cdk . aws_ec2 . Subnet ( this , 'Elastic-IP-Lambda-Subnet' , {
23- vpcId : vpc . vpcId ,
24- availabilityZone : props . availabilityZone || 'us-east-1e' ,
25- cidrBlock : props . cidrBlock || '172.31.96.0/20' ,
26- mapPublicIpOnLaunch : true ,
27- } ) ;
28- const routeTableId = props . routeTableId || vpc . publicSubnets [ 0 ] . routeTable . routeTableId ;
29- const routeTableAssociation = new cdk . aws_ec2 . CfnSubnetRouteTableAssociation ( this , 'rt-subnet-association' , {
30- subnetId : publicSubnet . subnetId ,
31- routeTableId,
32- } ) ;
33- const securityGroup = new cdk . aws_ec2 . SecurityGroup ( this , 'Elastic-IP-Lambda-Security-Group' , {
34- vpc,
35- allowAllOutbound : true ,
36- description :
37- 'This is a security group for a vpc attached lambda that uses elastic ip to have outbound communication without the need for a NAT solution' ,
38- } ) ;
21+ const vpc = cdk . aws_ec2 . Vpc . fromLookup ( this , 'Default-VPC' , { isDefault : ! props . vpcId , vpcId : props . vpcId } ) ;
22+ const securityGroup = ! ! props . securityGroupId
23+ ? cdk . aws_ec2 . SecurityGroup . fromLookupById ( this , 'Elastic-IP-Lambda-Security-Group' , props . securityGroupId )
24+ : new cdk . aws_ec2 . SecurityGroup ( this , 'Elastic-IP-Lambda-Security-Group' , {
25+ vpc,
26+ allowAllOutbound : true ,
27+ description :
28+ 'This is a security group for a vpc attached lambda that uses elastic ip to have outbound communication without the need for a NAT solution' ,
29+ } ) ;
3930
40- const publicFunction = new cdk . aws_lambda_nodejs . NodejsFunction ( this , 'Lambda-With-Elastic-IP' , {
41- memorySize : 128 ,
42- handler : 'handler' ,
43- timeout : cdk . Duration . seconds ( 10 ) ,
44- bundling : {
45- minify : true ,
46- sourceMap : true ,
47- sourceMapMode : cdk . aws_lambda_nodejs . SourceMapMode . DEFAULT , // defaults to SourceMapMode.DEFAULT
48- } ,
49- vpc,
50- securityGroups : [ securityGroup ] ,
51- allowPublicSubnet : true ,
52- vpcSubnets : { subnets : [ publicSubnet ] } ,
53- runtime : cdk . aws_lambda . Runtime . NODEJS_18_X ,
54- entry : 'src/lambdas/vin-api-lambda.ts' ,
55- } ) ;
31+ const publicSubnet = ! ! props . subnetId
32+ ? cdk . aws_ec2 . Subnet . fromSubnetId ( this , 'Elastic-IP-Lambda-Subnet' , props . subnetId )
33+ : vpc . publicSubnets [ 0 ] ;
5634
57- const elasticIP = new cdk . aws_ec2 . CfnEIP ( this , 'Lambda-Elastic-Ip' , { } ) ;
35+ const publicFunction = new cdk . aws_lambda_nodejs . NodejsFunction ( this , 'Lambda-With-Elastic-IP' , {
36+ memorySize : 128 ,
37+ handler : 'handler' ,
38+ timeout : cdk . Duration . seconds ( 10 ) ,
39+ bundling : {
40+ minify : true ,
41+ sourceMap : true ,
42+ sourceMapMode : cdk . aws_lambda_nodejs . SourceMapMode . DEFAULT , // defaults to SourceMapMode.DEFAULT
43+ } ,
44+ vpc,
45+ securityGroups : [ securityGroup ] ,
46+ allowPublicSubnet : true ,
47+ vpcSubnets : { subnets : [ publicSubnet ] } ,
48+ runtime : cdk . aws_lambda . Runtime . NODEJS_18_X ,
49+ entry : 'src/lambdas/vin-api-lambda.ts' ,
50+ } ) ;
5851
59- this . associateLambdaToElasticIpCR ( {
60- elasticIP,
61- vpc,
62- publicSubnet,
63- securityGroup,
64- functionName : publicFunction . functionName ,
65- } ) ;
66- }
52+ const elasticIP = new cdk . aws_ec2 . CfnEIP ( this , 'Lambda-Elastic-Ip' , { } ) ;
6753
68- private associateLambdaToElasticIpCR ( {
69- elasticIP,
70- publicSubnet,
71- securityGroup,
72- vpc,
73- functionName,
74- } : AssociateLambdaToElasticIpCRProps ) {
75- const associateElasticIpFunctionCR = new cdk . aws_lambda_nodejs . NodejsFunction ( this , 'Associate-Elastic-IP-CR' , {
76- memorySize : 128 ,
77- handler : 'handler' ,
78- timeout : cdk . Duration . seconds ( 10 ) ,
79- bundling : {
80- minify : true ,
81- sourceMap : true ,
82- sourceMapMode : cdk . aws_lambda_nodejs . SourceMapMode . DEFAULT , // defaults to SourceMapMode.DEFAULT
83- } ,
84- environment : {
85- ELASTIC_IP_ALLOCATION_ID : elasticIP . attrAllocationId ,
86- } ,
87- runtime : cdk . aws_lambda . Runtime . NODEJS_18_X ,
88- entry : 'src/lambdas/associate-lambda-elastic-ip-cr.ts' ,
89- } ) ;
54+ this . associateLambdaToElasticIpCR ( {
55+ elasticIP,
56+ vpc,
57+ publicSubnet,
58+ securityGroup,
59+ functionName : publicFunction . functionName ,
60+ } ) ;
61+ }
9062
91- associateElasticIpFunctionCR . addToRolePolicy (
92- new cdk . aws_iam . PolicyStatement ( {
93- effect : cdk . aws_iam . Effect . ALLOW ,
94- actions : [ 'ec2:AssociateAddress' , 'ec2:DescribeNetworkInterfaces' ] ,
95- resources : [ '*' ] ,
96- } )
97- ) ;
63+ private associateLambdaToElasticIpCR ( { elasticIP, publicSubnet, securityGroup, vpc, functionName } : AssociateLambdaToElasticIpCRProps ) {
64+ const associateElasticIpFunctionCR = new cdk . aws_lambda_nodejs . NodejsFunction ( this , 'Associate-Elastic-IP-CR' , {
65+ memorySize : 128 ,
66+ handler : 'handler' ,
67+ timeout : cdk . Duration . seconds ( 10 ) ,
68+ bundling : {
69+ minify : true ,
70+ sourceMap : true ,
71+ sourceMapMode : cdk . aws_lambda_nodejs . SourceMapMode . DEFAULT , // defaults to SourceMapMode.DEFAULT
72+ } ,
73+ environment : {
74+ ELASTIC_IP_ALLOCATION_ID : elasticIP . attrAllocationId ,
75+ } ,
76+ runtime : cdk . aws_lambda . Runtime . NODEJS_18_X ,
77+ entry : 'src/lambdas/associate-lambda-elastic-ip-cr.ts' ,
78+ } ) ;
9879
99- const awsSDKCall : cdk . custom_resources . AwsSdkCall = {
100- service : 'Lambda' ,
101- action : 'invoke' ,
102- parameters : {
103- FunctionName : associateElasticIpFunctionCR . functionName ,
104- Payload : JSON . stringify ( {
105- vpcId : vpc . vpcId ,
106- subnetId : publicSubnet . subnetId ,
107- securityGroupId : securityGroup . securityGroupId ,
108- availabilityZone : publicSubnet . availabilityZone ,
109- allocationId : elasticIP . attrAllocationId ,
110- staticIp : elasticIP . ref ,
111- functionName,
112- date : new Date ( ) ,
113- } ) ,
114- } ,
115- physicalResourceId : cdk . custom_resources . PhysicalResourceId . of ( `Associate-Elastic-IP-CR` ) ,
116- } ;
117- const customResource = new cdk . custom_resources . AwsCustomResource ( this , 'Invoke-Associate-Elastic-IP-Lambda' , {
118- onCreate : awsSDKCall ,
119- onUpdate : awsSDKCall ,
120- policy : cdk . custom_resources . AwsCustomResourcePolicy . fromSdkCalls ( {
121- resources : cdk . custom_resources . AwsCustomResourcePolicy . ANY_RESOURCE ,
122- } ) ,
123- } ) ;
80+ associateElasticIpFunctionCR . addToRolePolicy (
81+ new cdk . aws_iam . PolicyStatement ( {
82+ effect : cdk . aws_iam . Effect . ALLOW ,
83+ actions : [ 'ec2:AssociateAddress' , 'ec2:DescribeNetworkInterfaces' ] ,
84+ resources : [ '*' ] ,
85+ } ) ,
86+ ) ;
12487
125- associateElasticIpFunctionCR . grantInvoke ( customResource ) ;
126- }
88+ const awsSDKCall : cdk . custom_resources . AwsSdkCall = {
89+ service : 'Lambda' ,
90+ action : 'invoke' ,
91+ parameters : {
92+ FunctionName : associateElasticIpFunctionCR . functionName ,
93+ Payload : JSON . stringify ( {
94+ vpcId : vpc . vpcId ,
95+ subnetId : publicSubnet . subnetId ,
96+ securityGroupId : securityGroup . securityGroupId ,
97+ availabilityZone : publicSubnet . availabilityZone ,
98+ allocationId : elasticIP . attrAllocationId ,
99+ staticIp : elasticIP . ref ,
100+ functionName,
101+ date : new Date ( ) ,
102+ } ) ,
103+ } ,
104+ physicalResourceId : cdk . custom_resources . PhysicalResourceId . of ( `Associate-Elastic-IP-CR` ) ,
105+ } ;
106+ const customResource = new cdk . custom_resources . AwsCustomResource ( this , 'Invoke-Associate-Elastic-IP-Lambda' , {
107+ onCreate : awsSDKCall ,
108+ onUpdate : awsSDKCall ,
109+ policy : cdk . custom_resources . AwsCustomResourcePolicy . fromSdkCalls ( {
110+ resources : cdk . custom_resources . AwsCustomResourcePolicy . ANY_RESOURCE ,
111+ } ) ,
112+ } ) ;
113+
114+ associateElasticIpFunctionCR . grantInvoke ( customResource ) ;
115+ }
127116}
0 commit comments