diff --git a/{{cookiecutter.project_slug}}/.ebextensions/05_packages.config b/{{cookiecutter.project_slug}}/.ebextensions/10_packages.config similarity index 100% rename from {{cookiecutter.project_slug}}/.ebextensions/05_packages.config rename to {{cookiecutter.project_slug}}/.ebextensions/10_packages.config diff --git a/{{cookiecutter.project_slug}}/.ebextensions/20_elasticcache.config b/{{cookiecutter.project_slug}}/.ebextensions/20_elasticcache.config new file mode 100644 index 000000000..539f55094 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.ebextensions/20_elasticcache.config @@ -0,0 +1,46 @@ +#This sample requires you to create a separate configuration file that defines the custom +# option settings for CacheCluster properties. + +Resources: + MyCacheSecurityGroup: + Type: "AWS::EC2::SecurityGroup" + Properties: + GroupDescription: "Lock cache down to webserver access only" + SecurityGroupIngress : + - IpProtocol : "tcp" + FromPort : + Fn::GetOptionSetting: + OptionName : "CachePort" + DefaultValue: "6379" + ToPort : + Fn::GetOptionSetting: + OptionName : "CachePort" + DefaultValue: "6379" + SourceSecurityGroupName: + Ref: "AWSEBSecurityGroup" + MyElastiCache: + Type: "AWS::ElastiCache::CacheCluster" + Properties: + CacheNodeType: + Fn::GetOptionSetting: + OptionName : "CacheNodeType" + DefaultValue : "cache.t1.micro" + NumCacheNodes: + Fn::GetOptionSetting: + OptionName : "NumCacheNodes" + DefaultValue : "1" + Engine: + Fn::GetOptionSetting: + OptionName : "Engine" + DefaultValue : "redis" + VpcSecurityGroupIds: + - + Fn::GetAtt: + - MyCacheSecurityGroup + - GroupId + +Outputs: + ElastiCache: + Description : "ID of ElastiCache Cache Cluster with Redis Engine" + Value : + Ref : "MyElastiCache" diff --git a/{{cookiecutter.project_slug}}/.ebextensions/30_options.config b/{{cookiecutter.project_slug}}/.ebextensions/30_options.config new file mode 100644 index 000000000..d7135c3f9 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.ebextensions/30_options.config @@ -0,0 +1,6 @@ +option_settings: + "aws:elasticbeanstalk:customoption": + CacheNodeType : cache.t1.micro + NumCacheNodes : 1 + Engine : redis + CachePort : 6379 diff --git a/{{cookiecutter.project_slug}}/.ebextensions/10_python.config b/{{cookiecutter.project_slug}}/.ebextensions/40_python.config similarity index 66% rename from {{cookiecutter.project_slug}}/.ebextensions/10_python.config rename to {{cookiecutter.project_slug}}/.ebextensions/40_python.config index 737e71bb2..61b29375a 100644 --- a/{{cookiecutter.project_slug}}/.ebextensions/10_python.config +++ b/{{cookiecutter.project_slug}}/.ebextensions/40_python.config @@ -4,6 +4,10 @@ container_commands: leader_only: True 02_collectstatic: command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput" +option_settings: + "aws:elasticbeanstalk:application:environment": + REDIS_ENDPOINT_ADDRESS: '`{ "Fn::GetAtt" : [ "MyElastiCache", "RedisEndpoint.Address"]}`' + REDIS_PORT: '`{ "Fn::GetAtt" : [ "MyElastiCache", "RedisEndpoint.Port"]}`' option_settings: "aws:elasticbeanstalk:container:python": WSGIPath: "config/wsgi.py" diff --git a/{{cookiecutter.project_slug}}/config/settings/production.py b/{{cookiecutter.project_slug}}/config/settings/production.py index 04a9426fd..57e00caa0 100644 --- a/{{cookiecutter.project_slug}}/config/settings/production.py +++ b/{{cookiecutter.project_slug}}/config/settings/production.py @@ -177,16 +177,39 @@ TEMPLATES[0]['OPTIONS']['loaders'] = [ # DATABASE CONFIGURATION # ------------------------------------------------------------------------------ +{% if cookiecutter.use_elasticbeanstalk -%} +# Uses Amazon RDS for database hosting, which doesn't follow the Heroku-style spec +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': env('RDS_DB_NAME'), + 'USER': env('RDS_USERNAME'), + 'PASSWORD': env('RDS_PASSWORD'), + 'HOST': env('RDS_HOSTNAME'), + 'PORT': env('RDS_PORT'), + } +} +{% else %} +# Use the Heroku-style specification # Raises ImproperlyConfigured exception if DATABASE_URL not in os.environ DATABASES['default'] = env.db('DATABASE_URL') +{%- endif %} # CACHING # ------------------------------------------------------------------------------ +{% if cookiecutter.use_elasticbeanstalk -%} +REDIS_LOCATION = "redis://{}:{}/0".format( + env('REDIS_ENDPOINT_ADDRESS'), + env('REDIS_PORT') +) +{% else %} +REDIS_LOCATION = '{0}/{1}'.format(env('REDIS_URL', default='redis://127.0.0.1:6379'), 0) +{%- endif %} # Heroku URL does not pass the DB number, so we parse it in CACHES = { 'default': { 'BACKEND': 'django_redis.cache.RedisCache', - 'LOCATION': '{0}/{1}'.format(env('REDIS_URL', default='redis://127.0.0.1:6379'), 0), + 'LOCATION': REDIS_LOCATION, 'OPTIONS': { 'CLIENT_CLASS': 'django_redis.client.DefaultClient', 'IGNORE_EXCEPTIONS': True, # mimics memcache behavior.