Nayden Van
Nayden Van

Reputation: 1569

aws terraform seed cloud config file during deployment

I have an infra that I create with terraform which has the following resources:

The entire infra, has been developed using modules that I wrote. During the deployment I am provisioning the ec2 with nginx and writing a nginx.conf file using #cloud-config as follow:

Template_file

package_update: true
package_upgrade: false

packages:
  - nginx

write_files:
  - content: |
        # This is the new file
        user www-data;
        worker_processes auto;
        pid /run/nginx.pid;

        events {
            worker_connections 768;
            # multi_accept on;
        }

        http {
            server_names_hash_bucket_size 128;
            server {
                listen   80; ## listen for ipv4; this line is default and implied
                server_name ${output.elb_dns_name};
                root /usr/share/nginx/html;
                index index.html;

                server_tokens  off; # disable the Server nginx header 

                # enable gzip
                gzip on;
                gzip_disable "msie6";

                gzip_comp_level 6;
                gzip_min_length 1100;
                gzip_buffers 16 8k;
                gzip_proxied any;
                gzip_types
                    text/plain
                    text/css
                    text/js
                    text/xml
                    text/javascript
                    application/javascript
                    application/x-javascript
                    application/json
                    application/xml
                    application/rss+xml
                    image/svg+xml;

                location / {
                    # try_files $uri /index.html; # redirect all request to index.html
                    proxy_pass <my-domain>;

                }
            }
            ##
            # Basic Settings
            ##

            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            keepalive_timeout 65;
            types_hash_max_size 2048;
            # server_tokens off;

            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;

            include /etc/nginx/mime.types;
            default_type application/octet-stream;

            ##
            # SSL Settings
            ##

            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
            ssl_prefer_server_ciphers on;

            ##
            # Logging Settings
            ##

            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;

            ##
            # Gzip Settings
            ##

            gzip on;
            gzip_disable "msie6";

            # gzip_vary on;
            # gzip_proxied any;
            # gzip_comp_level 6;
            # gzip_buffers 16 8k;
            # gzip_http_version 1.1;
            # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

            ##
            # Virtual Host Configs
            ##

            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
        }


        #mail {
        #   # See sample authentication script at:
        #   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
        # 
        #   # auth_http localhost/auth.php;
        #   # pop3_capabilities "TOP" "USER";
        #   # imap_capabilities "IMAP4rev1" "UIDPLUS";
        # 
        #   server {
        #       listen     localhost:110;
        #       protocol   pop3;
        #       proxy      on;
        #   }
        # 
        #   server {
        #       listen     localhost:143;
        #       protocol   imap;
        #       proxy      on;
        #   }
        #}
    path: /etc/nginx/nginx.conf

runcmd:
- nginx -s reload

The purpose of this educational project, is to learn how to use a load balancer which an nginx reverse proxy.

Everything works fine but when I create the instance and write_file the server_nameis hardcoded, which mean the load-balancer dns name won't match.

Using terraform I am able to extract the ELB dns_name in the output, but I was wondering how I can seed this output in the cloud config file so it will always pick up the correct dns name?

Thank you so much for your help/hint.

UPDATE:

Data template file this is the template file data for nginx config.

data "template_file" "nginx" {
  template = file("./template/nginx.yaml")
}

In my output.tf I set this output:

output "elb_dns_name" {
  value = module.load-balancer.ELB
}

if I run terraform apply I can see the output. so I tried to use this as a placeholder in my conf file as this:

            server_names_hash_bucket_size 128;
            server {
                listen   80; ## listen for ipv4; this line is default and implied
                server_name ${elb_dns_name};
                root /usr/share/nginx/html;
                index index.html;

but if I run terraform apply, I get the following error:

Error: failed to render : <template_file>:26,31-37: Unknown variable; There is no variable named "output".

  on dev.tf line 4, in data "template_file" "nginx":
   4: data "template_file" "nginx" {

Upvotes: 0

Views: 327

Answers (1)

Nayden Van
Nayden Van

Reputation: 1569

I found my problem.

My data template file was missing the vars. I added the variable as follow:

data "template_file" "nginx" {
  template = file("../dev/template/nginx.yaml")
  vars = {
    "output" = module.load-balancer.ELB
      
  }
}

And in my nginx.yaml I passed the output variable declare as follow:

            server {
                listen   80; ## listen for ipv4; this line is default and implied
                server_name ${output};
                root /usr/share/nginx/html;
                index index.html;

And I was able to seed dynamically the yaml file with the output value.

Upvotes: 0

Related Questions