1
votes

I have a system that requires an EC2 instance that has connections to a DynamoDB table and a Redis instance. How do I set those up as options for my EC2 instance in CloudFormation using YAML?

I've created OptionSettings to set the values as environmental variable for my process on the EC2 instance. I know that I need to do !Ref [something here], but the documentation did not have examples that made sense to me.

Here's a simplified version of the code:

---
AWSTemplateFormatVersion: "2010-09-09"

Description: CloudFormation template

Resources:
  # Beanstalk application
  MyApp:
    Type: AWS::ElasticBeanstalk::Application
  ConfigurationTemplate:
    Type: AWS::ElasticBeanstalk::ConfigurationTemplate
    Properties:
      ApplicationName: !Ref MyApp
      OptionSettings:
      - Namespace: "aws:elasticbeanstalk:application:environment"
        OptionName: REDIS_CACHE_PORT
        Value: !Ref AttributeCache.Port # Should be 6379
      - Namespace: "aws:elasticbeanstalk:application:environment"
        OptionName: REDIS_CACHE_ADDRESS
        Value: !Ref MyCache. # Something that AWS creates
      - Namespace: "aws:elasticbeanstalk:application:environment"
        OptionName: DYNAMODB_TABLE_ADDRESS
        Value: !Ref MyDynamoTable. # Something that AWS creates
      SolutionStackName: 64bit Amazon Linux 2018.03 v4.8.1 running Node.js

  # Redis cache
  MyCache:
    Type: AWS::ElastiCache::CacheCluster
    Properties:
      ClusterName: MyCacheCluster
      Engine: redis
      EngineVersion: 4.0.10
      NumCacheNodes: 1
      Port: 6379

  # DynamoDB table
  MyDynamoTable:
    Type: "AWS::DynamoDB::Table"
    Properties:
      AttributeDefinitions:
      - AttributeName: theAttributeName
        AttributeType: S
      KeySchema:
      - AttributeName: theAttributeName
        KeyType: HASH
      TableName: myDynamoTable

I am unsure of how to access something that CloudFormation will create. Is !Ref correct, or do I need the !GetAtt instead?

Here are the links to the two different function docs.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html

Even just pointing me at the thing that I'm missing would be of help.

1

1 Answers

2
votes

This kind of information is specified under Return Values section in the documentation for every service. This doc section also provides information whether you have to use !Ref or !GetAtt in order to retrieve certain parameter. For example CacheCluster docs say that you have to use !GetAtt MyCache.RedisEndpoint.Port to get redis' port and Dynamo docs say that you have to use !Ref MyDynamoTable to get table name.

      OptionSettings:
      - Namespace: "aws:elasticbeanstalk:application:environment"
        OptionName: REDIS_CACHE_PORT
        Value: !GetAtt MyCache.RedisEndpoint.Port
      - Namespace: "aws:elasticbeanstalk:application:environment"
        OptionName: REDIS_CACHE_ADDRESS
        Value: !GetAtt MyCache.RedisEndpoint.Address
      - Namespace: "aws:elasticbeanstalk:application:environment"
        OptionName: DYNAMODB_TABLE_ADDRESS
        Value: !Ref MyDynamoTable # You got this one right, but no dot in the end