|
| 1 | +import { Duration, Stack, StackProps } from 'aws-cdk-lib'; |
| 2 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 3 | +import * as lambda from 'aws-cdk-lib/aws-lambda'; |
| 4 | +import * as ec2 from 'aws-cdk-lib/aws-ec2'; |
| 5 | +import * as redis from 'aws-cdk-lib/aws-elasticache'; |
| 6 | +import { Construct } from 'constructs'; |
| 7 | + |
| 8 | +export class LambdaElasticacheIntegrationpatternCdkStack extends Stack { |
| 9 | + constructor(scope: Construct, id: string, props?: StackProps) { |
| 10 | + super(scope, id, props); |
| 11 | + // defines an AWS Lambda resource roles |
| 12 | + const lambdarole = new iam.Role(this,'lambda-vpc-execution-role',{ |
| 13 | + assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 14 | + description: 'Lambda execution role for accessing VPC', |
| 15 | + managedPolicies: [ |
| 16 | + iam.ManagedPolicy.fromAwsManagedPolicyName( |
| 17 | + 'service-role/AWSLambdaVPCAccessExecutionRole', |
| 18 | + ), |
| 19 | + ], |
| 20 | + }); |
| 21 | + |
| 22 | + //get default or any private vpc |
| 23 | + const defaultvpc = ec2.Vpc.fromLookup(this, 'ElastiCacheVPC', { |
| 24 | + vpcName: "Default", // can be configured where ElastiCache is deployed |
| 25 | + isDefault: true |
| 26 | + }); |
| 27 | + |
| 28 | + //security group for lambda vpc access |
| 29 | + const lambdasecuritygroup = new ec2.SecurityGroup(this, 'LambdaVPC-SG',{ |
| 30 | + vpc:defaultvpc, |
| 31 | + allowAllOutbound: true, |
| 32 | + description: 'Security group for lambda to access Redis' |
| 33 | + }); |
| 34 | + |
| 35 | + //get predefined securitygroup |
| 36 | + const redissecuritygroup = new ec2.SecurityGroup(this, 'Redis-SG',{ |
| 37 | + vpc:defaultvpc, |
| 38 | + allowAllOutbound: true, |
| 39 | + description: 'Security group for Redis' |
| 40 | + }); |
| 41 | + redissecuritygroup.addIngressRule( |
| 42 | + ec2.Peer.securityGroupId(lambdasecuritygroup.securityGroupId), |
| 43 | + ec2.Port.tcp(6379), |
| 44 | + ); |
| 45 | + |
| 46 | + |
| 47 | + // Get all public subnet ids, you can deploy it to privatesubnets as well |
| 48 | + const Subnets = defaultvpc.publicSubnets.map((subnet) => { |
| 49 | + return subnet.subnetId |
| 50 | + }); |
| 51 | + |
| 52 | + // Create redis subnet group from subnet ids |
| 53 | + const redisSubnetGroup = new redis.CfnSubnetGroup(this, 'RedisSubnetGroup', { |
| 54 | + subnetIds: Subnets, |
| 55 | + description: "Subnet group for redis" |
| 56 | + }); |
| 57 | + |
| 58 | + // Create Redis Cluster |
| 59 | + const redisCluster = new redis.CfnCacheCluster(this, 'RedisCluster', { |
| 60 | + autoMinorVersionUpgrade: true, |
| 61 | + cacheNodeType: 'cache.t2.small', |
| 62 | + engine: 'redis', |
| 63 | + numCacheNodes: 1, |
| 64 | + cacheSubnetGroupName: redisSubnetGroup.ref, |
| 65 | + clusterName: 'sample-redis' , |
| 66 | + vpcSecurityGroupIds: [redissecuritygroup.securityGroupId] |
| 67 | + }); |
| 68 | + |
| 69 | + // Define this redis cluster is depends on redis subnet group created first |
| 70 | + redisCluster.node.addDependency(redisSubnetGroup); |
| 71 | + |
| 72 | + // Lambda creation |
| 73 | + const redisaccess = new lambda.Function(this, 'Elasticache-RedisAccess', { |
| 74 | + runtime: lambda.Runtime.NODEJS_18_X, // execution environment |
| 75 | + code: lambda.Code.fromAsset('lambda'), // code loaded from "lambda" directory |
| 76 | + handler: 'index.handler', // file is "index", function is "handler" |
| 77 | + role: lambdarole, |
| 78 | + vpc:defaultvpc, |
| 79 | + allowPublicSubnet: true, |
| 80 | + securityGroups: [lambdasecuritygroup], |
| 81 | + timeout: Duration.minutes(5), |
| 82 | + environment: { |
| 83 | + REDIS_PORT: redisCluster.attrRedisEndpointPort, |
| 84 | + REDIS_HOST: redisCluster.attrRedisEndpointAddress, |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments