Skip to content

Commit

Permalink
Change boot type
Browse files Browse the repository at this point in the history
  • Loading branch information
carlssonk committed Sep 26, 2024
1 parent f18aae0 commit f8e38a0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
4 changes: 2 additions & 2 deletions common/services/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module "service_discovery_namespace" {
namespace_name = module.globals.var.organization
}

module "ec2_instance_nginx_proxy" {
module "ec2_instance_nginx" {
count = var.reverse_proxy_type == "nginx" ? 1 : 0
name = "nginx-reverse-proxy"
source = "../../modules/ec2-instance/default"
Expand Down Expand Up @@ -62,7 +62,7 @@ module "ec2_instance_nginx_proxy" {
module "ec2_instance_nginx_eip" {
count = var.reverse_proxy_type == "nginx" ? 1 : 0
source = "../../modules/elastic-ip/default"
instance_id = module.ec2_instance_nginx_proxy[0].id
instance_id = module.ec2_instance_nginx[0].id
}

module "main_alb_access_logs_bucket" {
Expand Down
24 changes: 9 additions & 15 deletions common/services/nginx_reverse_proxy.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#!/bin/bash

yum update -y
if ! rpm -q nginx certbot python3-certbot-nginx > /dev/null; then
yum install -y nginx certbot python3-certbot-nginx
fi
yum install -y nginx certbot python3-certbot-nginx

# Create nginx config
sudo tee /etc/nginx/nginx.conf <<EOF
Expand All @@ -14,7 +12,7 @@ events {

http {
map $\http_host $\upstream {
map $http_host $upstream {
hostnames;
%{ for domain, backend in services_map ~}
${domain} ${backend};
Expand All @@ -25,7 +23,7 @@ http {
server {
listen 80;
server_name _;
return 301 https://$\host$\request_uri;
return 301 https://$host$request_uri;
}
server {
Expand All @@ -43,22 +41,18 @@ http {
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
location / {
proxy_pass http://$\upstream;
proxy_set_header Host $\host;
proxy_set_header X-Real-IP $\remote_addr;
proxy_set_header X-Forwarded-For $\proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $\scheme;
proxy_pass http://$upstream;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
EOF

# Obtain SSL certificate
if ! certbot certificates | grep -q "${certbot_domains}"; then
certbot --nginx -d ${certbot_domains} --non-interactive --agree-tos -m [email protected]
else
certbot renew --nginx --non-interactive
fi
certbot --nginx -d ${certbot_domains} --non-interactive --agree-tos -m [email protected]

# Ensure Certbot auto-renewal is enabled
systemctl enable certbot.timer
Expand Down
2 changes: 1 addition & 1 deletion common/services/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
output "policy_documents" {
value = [
try(module.service_discovery_namespace[0].policy_document, null),
try(module.ec2_instance_nginx_proxy[0].policy_document, null),
try(module.ec2_instance_nginx[0].policy_document, null),
try(module.ec2_instance_nginx_eip[0].policy_document, null),
try(module.main_alb[0].policy_document, null),
try(module.main_alb_access_logs_bucket[0].policy_document, null),
Expand Down
12 changes: 12 additions & 0 deletions common/services/run_every_boot copy.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#cloud-config
cloud_final_modules:
- [scripts-user, always]

write_files:
- path: /var/lib/cloud/scripts/per-boot/user-script.sh
permissions: '0755'
content: |
${indent(8, nginx_config)}

runcmd:
- /var/lib/cloud/scripts/per-boot/user-script.sh
25 changes: 17 additions & 8 deletions common/services/run_every_boot.tpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]
- [scripts-user, always]

write_files:
- path: /var/lib/cloud/scripts/per-boot/user-script.sh
permissions: '0755'
content: |
${indent(8, nginx_config)}
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

runcmd:
- /var/lib/cloud/scripts/per-boot/user-script.sh
${nginx_config}
--//

0 comments on commit f8e38a0

Please sign in to comment.