title: How do I use CloudFormation resources in a Lambda function?
body: <p>I have added a Redis ElastiCache section to my s-resource-cf.json (a CloudFormation template), and selected its hostname as an output.</p>
  
  <pre><code>"Resources": {
      ...snip...
      "Redis": {
          "Type": "AWS::ElastiCache::CacheCluster",
          "Properties": {
              "AutoMinorVersionUpgrade": "true",
              "AZMode": "single-az",
              "CacheNodeType": "cache.t2.micro",
              "Engine": "redis",
              "EngineVersion": "2.8.24",
              "NumCacheNodes": "1",
              "PreferredAvailabilityZone": "eu-west-1a",
              "PreferredMaintenanceWindow": "tue:00:30-tue:01:30",
              "CacheSubnetGroupName": {
                  "Ref": "cachesubnetdefault"
              },
              "VpcSecurityGroupIds": [
                  {
                      "Fn::GetAtt": [
                          "sgdefault",
                          "GroupId"
                      ]
                  }
              ]
          }
      }
  },
  "Outputs": {
      "IamRoleArnLambda": {
          "Description": "ARN of the lambda IAM role",
          "Value": {
              "Fn::GetAtt": [
                  "IamRoleLambda",
                  "Arn"
              ]
          }
      },
      "RedisEndpointAddress": {
          "Description": "Redis server host",
          "Value": {
              "Fn::GetAtt": [
                  "Redis",
                  "Address"
              ]
          }
      }
  }
  </code></pre>
  
  <p>I can get CloudFormation to output the Redis server host when running <code>sls resources deploy</code>, but how can I access that output from within a Lambda function?</p>
  
  <p>There is nothing in this starter project template that refers to that <code>IamRoleArnLambda</code>, which came with the example project. According to the <a href="http://docs.serverless.com/docs/templates-variables" rel="nofollow">docs</a>, templates are only usable for project configuration, they are not accessible from Lambda functions:</p>
  
  <blockquote>
    <p><strong>Templates &amp; Variables are for Configuration Only</strong></p>
    
    <p>Templates and variables are used for configuration of the project only. This information is not usable in your lambda functions. To set variables which can be used by your lambda functions, use environment variables.</p>
  </blockquote>
  
  <p>So, then how do I set an environment variable to the hostname of the ElastiCache server after it has been created?</p>