Created
May 9, 2016 15:58
-
-
Save d4goxn/7322250ffe89f8a8a5c5d62804a8da2a to your computer and use it in GitHub Desktop.
Serverless CloudFormation config for Elasticache Redis server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "showConfig", | |
"runtime": "nodejs", | |
"description": "Returns Redis host and Serverless stage", | |
"handler": "redis/showConfig/handler.handler", | |
"timeout": 6, | |
"memorySize": 256, | |
"custom": { | |
"optimize": true | |
}, | |
"environment": { | |
"STAGE": "${stage}", | |
"REDIS_HOST": "${redisHost}" | |
}, | |
"endpoints": [{ | |
"path": "redis/showConfig", | |
"method": "POST", | |
"type": "AWS", | |
"requestParameters": {}, | |
"requestTemplates": "$${apiRequestTemplate}", | |
"responses": { | |
"default": { | |
"statusCode": "200" | |
} | |
} | |
}] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"AWSTemplateFormatVersion": "2010-09-09", | |
"Description": "The AWS CloudFormation template for this Serverless application's resources outside of Lambdas and Api Gateway", | |
"Resources": { | |
"IamRoleLambda": { | |
"Type": "AWS::IAM::Role", | |
"Properties": { | |
"AssumeRolePolicyDocument": { | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { | |
"Service": [ | |
"lambda.amazonaws.com" | |
] | |
}, | |
"Action": [ | |
"sts:AssumeRole" | |
] | |
} | |
] | |
}, | |
"Path": "/" | |
} | |
}, | |
"IamPolicyLambda": { | |
"Type": "AWS::IAM::Policy", | |
"Properties": { | |
"PolicyName": "${stage}-${project}-lambda", | |
"PolicyDocument": { | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"logs:CreateLogGroup", | |
"logs:CreateLogStream", | |
"logs:PutLogEvents" | |
], | |
"Resource": "arn:aws:logs:${region}:*:*" | |
}, | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"logs:CreateLogGroup", | |
"logs:CreateLogStream", | |
"logs:PutLogEvents", | |
"ec2:CreateNetworkInterface", | |
"ec2:DescribeNetworkInterfaces", | |
"ec2:DeleteNetworkInterface" | |
], | |
"Resource": "*" | |
} | |
] | |
}, | |
"Roles": [ | |
{ | |
"Ref": "IamRoleLambda" | |
} | |
] | |
} | |
}, | |
"RedisCluster" : { | |
"Type": "AWS::ElastiCache::CacheCluster", | |
"Properties": { | |
"CacheNodeType": "cache.t2.micro", | |
"CacheSecurityGroupNames": ["default"], | |
"Engine": "redis", | |
"NumCacheNodes": "1" | |
} | |
} | |
}, | |
"Outputs": { | |
"IamRoleArnLambda": { | |
"Description": "ARN of the lambda IAM role", | |
"Value": { | |
"Fn::GetAtt": [ | |
"IamRoleLambda", | |
"Arn" | |
] | |
} | |
}, | |
"RedisHost": { | |
"Description": "ARN of the lambda IAM role", | |
"Value": { | |
"Fn::GetAtt": [ | |
"RedisCluster", | |
"RedisEndpoint.Address" | |
] | |
} | |
} | |
} | |
} |
Weird how the "RedisHost" in the Outputs for CloudFormation has to be "redisHost" when defining the env variable. I noticed this after quite a while trying to get it to work. Maybe add a note at the top of your gist?
Thanks again!
Hi there, how can I use this for Replication Groups?
"ElasticacheReplicationGroup": {
"Type": "AWS::ElastiCache::ReplicationGroup",
"Properties": {
"ReplicationGroupDescription" : "Redis replication group",
"NumCacheClusters" : "2",
"Engine" : "redis",
"CacheNodeType" : "cache.t2.micro",
"AutoMinorVersionUpgrade" : "true",
"AutomaticFailoverEnabled" : "false"
}
}
I cannot use "CacheSecurityGroupNames": ["default"] because I get _
Your accounts support the EC2-VPC platform, which does not support ElastiCache security groups. Instead, specify VPC security group IDs.
_
Also, I tried exactly the RedisCluster version on the this Gist but I can't connect to Redis within the Lambda, can anyone please tell if this is currently working??
Did you manage to get it working?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks so much for this! Super useful!