Skip to content

Instantly share code, notes, and snippets.

@d4goxn
Created May 9, 2016 15:58
Show Gist options
  • Save d4goxn/7322250ffe89f8a8a5c5d62804a8da2a to your computer and use it in GitHub Desktop.
Save d4goxn/7322250ffe89f8a8a5c5d62804a8da2a to your computer and use it in GitHub Desktop.
Serverless CloudFormation config for Elasticache Redis server
{
"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"
}
}
}]
}
{
"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"
]
}
}
}
}
@em0ney
Copy link

em0ney commented May 12, 2016

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!

@karloscodes
Copy link

karloscodes commented Sep 29, 2016

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.

_

@karloscodes
Copy link

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??

@clstl
Copy link

clstl commented Jan 9, 2017

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