<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vanessa Vargas</title>
    <description>The latest articles on DEV Community by Vanessa Vargas (@vanevargas).</description>
    <link>https://dev.to/vanevargas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1541970%2F740cbede-e119-47b8-b133-d76f33f0e99e.jpg</url>
      <title>DEV Community: Vanessa Vargas</title>
      <link>https://dev.to/vanevargas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vanevargas"/>
    <language>en</language>
    <item>
      <title>Automate the renewal of a Let's Encrypt Certiticate with AWS Batch and Docker</title>
      <dc:creator>Vanessa Vargas</dc:creator>
      <pubDate>Tue, 18 Jun 2024 03:25:02 +0000</pubDate>
      <link>https://dev.to/vanevargas/automate-the-renewal-of-a-lets-encrypt-certiticate-with-aws-batch-and-docker-327c</link>
      <guid>https://dev.to/vanevargas/automate-the-renewal-of-a-lets-encrypt-certiticate-with-aws-batch-and-docker-327c</guid>
      <description>&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Certbot it's not currently installed in the web server so the certificate is generated somewhere else then copied to the web server&lt;/li&gt;
&lt;li&gt;The Route53 DNS method is currently used to manually renew the certificate&lt;/li&gt;
&lt;li&gt;The Route53 domain already existed so it doesn't need to be created&lt;/li&gt;
&lt;li&gt;Little to no maintenance after deployment of new infrastructure&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Technologies used
&lt;/h2&gt;

&lt;p&gt;A short description of the technoligies used.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Docker&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use a &lt;a href="https://hub.docker.com/r/certbot/dns-route53/tags" rel="noopener noreferrer"&gt;docker container&lt;/a&gt; with certbot and the dns-route53 plugin installed.&lt;/li&gt;
&lt;li&gt;Below is the command used to locally test the container:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    docker run --rm -it --env AWS_ACCESS_KEY_ID=ACCESS_KEY --env AWS_SECRET_ACCESS_KEY=SECRET_KEY -v "/c/letsencrypt:/etc/letsencrypt" certbot/dns-route53 certonly --dns-route53 -d DOMAIN.COM -m EMAIL@TEST.COM --agree-tos --non-interactive --server https://acme-v02.api.letsencrypt.org/directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;AWS Batch and EFS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS Batch Jobs can be used to create on-demand Fargate instances to run the Docker container.&lt;/li&gt;
&lt;li&gt;The problem is how to move the certificates created out of the container. The solution is to mount an EFS volume.&lt;/li&gt;
&lt;li&gt;The creation/management of the ECS clusters, tasks, tasks definitions is transparent to the user, everything is done in AWS Batch.
&lt;/li&gt;
&lt;li&gt;In summary (in the AWS Batch console), first you need to create a "Compute Environment", then a "Job Queue" and finally a "Job Definition". After that, you can run Jobs selecting the proper Job Definition and Queue.&lt;/li&gt;
&lt;li&gt;The video used to get a general idea on how AWS Batch works:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=weKeR-qg_-4" rel="noopener noreferrer"&gt;Batch can now use Fargate for a truly serverless experience&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;AWS Step Functions and Lambdas&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Join all the steps using Step Functions to execute the lambdas and invoke the batch jobs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;AWS IAM, S3 and SSM Parameters&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An S3 bucket is used to store the certificate copied from EFS.&lt;/li&gt;
&lt;li&gt;AWS IAM, a user is created along its access and secret keys. The credentials are used in the certbot command execution to create/renew the certificate.&lt;/li&gt;
&lt;li&gt;SSM Parameters, the parameters are used to store the access key and secret key from the user previously created. If these values are updated in the IAM User, the SSM parameters need to be updated too.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Terraform&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used to deploy the pre-requisites: IAM user, S3 bucket and SSM Parameters&lt;/li&gt;
&lt;li&gt;Used to deploy and update the AWS Batch infrastructure&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Serverless Framework&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used to the deploy and manage the the Step Functions and Lambda functions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Implementation description
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2Fimage1.png%3FupdatedAt%3D1716941510881" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2Fimage1.png%3FupdatedAt%3D1716941510881" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;An EventBridge rule is scheduled to start the Step Function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The step function process checks if the domain certificate needs to be renewed. It renews the certificate, copies it to S3, then copies it to the EC2 instance and restarts the httpd service using the SSM. Finally we get a list of the certificates.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below the Step Function process is detailed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2Fstepfunctions_graph.png%3FupdatedAt%3D1716944097432" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2Fstepfunctions_graph.png%3FupdatedAt%3D1716944097432" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;CheckCertificate: A Lambda function is executed to check how many days are left before the certificate expires. This information is retrieved from the domain itself, meaning we're not checking the certificate stored in EFS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ChoiceDaysLeft: A choice state is executed next. If the number of days is less or equal than 30 days then a renewal is executed, else only the certificate information is listed.&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F15984291%2F176079716-7aa9fc08-9744-4e02-b0ba-319be33aa0f3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F15984291%2F176079716-7aa9fc08-9744-4e02-b0ba-319be33aa0f3.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;RenewCertificates: The AWS Batch Job (using Fargate) to run the renewal command is executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2FCode_iQVc2jRUlX.png%3FupdatedAt%3D1716941756566" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2FCode_iQVc2jRUlX.png%3FupdatedAt%3D1716941756566" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;CopyToS3: All the files stored on EFS are copied to the S3 Bucket.   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2Ffirefox_XUUNGZV6kN.png%3FupdatedAt%3D1716941967715" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2Ffirefox_XUUNGZV6kN.png%3FupdatedAt%3D1716941967715" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GetFromS3: The files inside the "live" folder are copied to the instance to the proper path.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The Job to run the "certbot list" command is executed. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2Ffirefox_RPJzG1vsTU2.png%3FupdatedAt%3D1716943630193" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2F1risg4rd3n%2FAWS%2520Batch%2C%2520Docker%2520and%2520Let%27s%2520Encrypt%2Ffirefox_RPJzG1vsTU2.png%3FupdatedAt%3D1716943630193" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Setting up the Infrastructure
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: The code can be found in the GitHub &lt;a href="https://github.com/vane2804/aws-batch-cert-renewal" rel="noopener noreferrer"&gt;repo&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Follow the instructions to &lt;a href="https://developer.hashicorp.com/terraform/install" rel="noopener noreferrer"&gt;install Terraform&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deploy the pre-requisites folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Deploy the EFS infrastructure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update the file prod.tfvars with the proper variables.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Deploy the AWS Batch infrastructure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update the file prod.tfvars with the proper variables.&lt;/li&gt;
&lt;li&gt;Update the variable "fileSystemId" in the job definition files "create_certs_job.json", "list_certs_job.json", "renew_certs_job.json"&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Configure the IAM User and SSM Parameters&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create the access key and secret access key for the user "certbot_batch"&lt;/li&gt;
&lt;li&gt;Copy the values to the SSM Parameters: /certbot_batch/access_key and /certbot_batch/secret_key.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Deploy the Serverless Framework infrastructure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follow the instructions to &lt;a href="https://www.serverless.com/framework/docs-getting-started" rel="noopener noreferrer"&gt;install Serverless&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If needed, execute the "Configurations needed only once" steps&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configurations needed only once (the first execution)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The Batch Job to create a new certificate is only run once at the beginning, so certbot creates the folder infrastructure needed in EFS. The subsequent executions will be of the renewal Job.&lt;/li&gt;
&lt;li&gt;The current certificate in use in the instances was copied to EFS (after the folder structure is created).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The approach could be modified to copy the certificate directly to the EC2 instance so the S3 bucket is not needed.&lt;/li&gt;
&lt;li&gt;The process could be improved by adding notifications when the process fails.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Links Reviewed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://ongkhaiwei.medium.com/generate-lets-encrypt-certificate-with-dns-challenge-and-namecheap-e5999a040708" rel="noopener noreferrer"&gt;Generate Let’s Encrypt Certificate with DNS Challenge and Namecheap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://certbot-dns-route53.readthedocs.io/en/stable/" rel="noopener noreferrer"&gt;Welcome to certbot-dns-route53’s documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eff-certbot.readthedocs.io/en/stable/install.html#running-with-docker" rel="noopener noreferrer"&gt;Certbot-Running with Docker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/prog-code/lets-encrypt-wildcard-certificate-configuration-with-aws-route-53-9c15adb936a7" rel="noopener noreferrer"&gt;Let’s Encrypt Wildcard Certificate Configuration with AWS Route 53&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>learning</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
