<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Simon Bulmer on Medium]]></title>
        <description><![CDATA[Stories by Simon Bulmer on Medium]]></description>
        <link>https://medium.com/@ellishoile?source=rss-13adabaa3685------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*uEU4P-9EzmooYVzO</url>
            <title>Stories by Simon Bulmer on Medium</title>
            <link>https://medium.com/@ellishoile?source=rss-13adabaa3685------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 17:03:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ellishoile/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a scalable Virus Scanning Solution with AWS Lambda and ClamAV]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://awstip.com/building-a-scalable-virus-scanning-solution-with-aws-lambda-and-clamav-242c604ac731?source=rss-13adabaa3685------2"><img src="https://cdn-images-1.medium.com/max/1024/1*bKffjF0QHilHWXDumvfUzg.png" width="1024"></a></p><p class="medium-feed-snippet">In this article, I&#x2019;ll walk through creating a scalable virus-scanning solution using AWS Lambda, ClamAV, and Docker. The set up includes a&#x2026;</p><p class="medium-feed-link"><a href="https://awstip.com/building-a-scalable-virus-scanning-solution-with-aws-lambda-and-clamav-242c604ac731?source=rss-13adabaa3685------2">Continue reading on AWS Tip »</a></p></div>]]></description>
            <link>https://awstip.com/building-a-scalable-virus-scanning-solution-with-aws-lambda-and-clamav-242c604ac731?source=rss-13adabaa3685------2</link>
            <guid isPermaLink="false">https://medium.com/p/242c604ac731</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[learning]]></category>
            <dc:creator><![CDATA[Simon Bulmer]]></dc:creator>
            <pubDate>Fri, 03 Jan 2025 14:51:55 GMT</pubDate>
            <atom:updated>2025-01-13T19:11:22.948Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Automating EC2 Instance access using Python and AWS SSM]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://ellishoile.medium.com/automating-ec2-instance-access-using-python-and-aws-ssm-ac6d5b272b2f?source=rss-13adabaa3685------2"><img src="https://cdn-images-1.medium.com/max/1024/1*mDlmZNFNB-m2oGhyCj1sfw.png" width="1024"></a></p><p class="medium-feed-snippet">Accessing Amazon EC2 instances efficiently can be a challenge, especially when you need to handle multiple instances and manage various&#x2026;</p><p class="medium-feed-link"><a href="https://ellishoile.medium.com/automating-ec2-instance-access-using-python-and-aws-ssm-ac6d5b272b2f?source=rss-13adabaa3685------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://ellishoile.medium.com/automating-ec2-instance-access-using-python-and-aws-ssm-ac6d5b272b2f?source=rss-13adabaa3685------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac6d5b272b2f</guid>
            <category><![CDATA[sre]]></category>
            <category><![CDATA[it]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[aws-ssm]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Simon Bulmer]]></dc:creator>
            <pubDate>Fri, 12 Apr 2024 12:16:23 GMT</pubDate>
            <atom:updated>2024-04-12T12:32:14.321Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Amazon S3 Python delete]]></title>
            <link>https://ellishoile.medium.com/amazon-s3-python-delete-43589f240c1c?source=rss-13adabaa3685------2</link>
            <guid isPermaLink="false">https://medium.com/p/43589f240c1c</guid>
            <category><![CDATA[boto3]]></category>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[delete]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Simon Bulmer]]></dc:creator>
            <pubDate>Tue, 05 Oct 2021 11:03:47 GMT</pubDate>
            <atom:updated>2021-10-05T11:03:47.819Z</atom:updated>
            <content:encoded><![CDATA[<p>There are a handful of options available to AWS users if and when they want to delete objects in an S3 bucket. For the task I had, I took it as an opportunity to write something in Python using the boto3 libraries, and albeit very basic, it did the job.</p><p>There were literally thousands of objects to remove from a number of prefixes in the target bucket, and with versioning enabled I also had to make sure it deleted these as well.</p><pre>import boto3<br>import sys</pre><pre>s3 = boto3.resource(&#39;s3&#39;)<br>bucket = s3.Bucket(&quot;simon-bucket&quot;)<br>prefix = str(sys.argv[1])</pre><pre>for object_summary in bucket.objects.filter(Prefix=prefix):<br>    object_summary.delete()<br>    print(object_summary.key)</pre><pre>for version_summary in bucket.object_versions.filter(Prefix=prefix):<br>    version_summary.delete()<br>    print(version_summary.key)</pre><p>As stated in the opening block this was a very basic Python script with no error handling. After working through a number of the objects the script burst into flames (added for dramatic effects) and stopped working. A quick review later I found that the S3 bucket contained prefixes with special characters present and the script simply did not know how to handle it.</p><p>In other words, it is similar to executing a terminal command containing spaces and the terminal not knowing where the filename starts and ends. e.g <strong>aws s3 cp s3://simon-bucket/this is^a file”name.txt s3://simon-bucket1/this is^also a file”name.txt.</strong></p><p>I know the above example is more of a copy statement, but it’s the only thing that popped into my head at the time of writing.</p><p>So, I fiddled around with it for a little while and kept getting the same error. Eventually, I tried adding a couple of if / elif statements containing the special character and it seemed to work. Is this the correct way of handling this? Possibly not! Did it work for me? Yes</p><pre>import boto3<br>import sys</pre><pre>s3 = boto3.resource(&#39;s3&#39;)<br>bucket = s3.Bucket(&quot;simon-bucket&quot;)<br>prefix = str(sys.argv[1])</pre><pre>for object_summary in bucket.objects.filter(Prefix=prefix):<br>    if &#39;is^&#39; in object_summary.key:<br>       object_summary.delete()<br>    elif &#39;file&quot;&#39; in object_summary.key:<br>       object_summary.delete()<br>    else:<br>       object_summary.delete()<br>       print(object_summary.key)</pre><pre>for version_summary in bucket.object_versions.filter(Prefix=prefix):<br>    if &#39;is^&#39; in version_summary.key:<br>       version_summary.delete()<br>    elif &#39;file&quot;&#39; in version_summary.key:<br>       version_summary.delete()<br>    else:<br>       version_summary.delete()<br>       print(version_summary.key)</pre><p>Not the best Python script in the world by far, but it did what I needed it to do. I execute my scripts mostly from a laptop and use <a href="https://github.com/mmmorris1975/aws-runas">aws-runas</a> tagged to the beginning of the command to handle my AWS session. When executing this script I allowed a single argument to be passed controlling the prefix input, allowing me to add to a bash script, but that’s not to say the bucket variable cannot be changed also.</p><p>At the moment this is just sat on my laptop collecting dust and I haven’t really spent any more time on it. I definitely can improve it moving forward, I just need to find the time to do so and in the meantime, I am just happy to share it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43589f240c1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building an AWS Transfer Family service]]></title>
            <link>https://ellishoile.medium.com/building-an-aws-transfer-family-service-db6858394c52?source=rss-13adabaa3685------2</link>
            <guid isPermaLink="false">https://medium.com/p/db6858394c52</guid>
            <category><![CDATA[aws-transfer-service]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[sftp]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Simon Bulmer]]></dc:creator>
            <pubDate>Thu, 25 Feb 2021 11:40:14 GMT</pubDate>
            <atom:updated>2021-02-26T07:52:49.904Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*h-8Mzh6XGWpmWM9U" /><figcaption>Photo by <a href="https://unsplash.com/@marvelous?utm_source=medium&amp;utm_medium=referral">Marvin Meyer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Specifically created to use a VPC Endpoint</h4><p>So recently I had to undertake the building of a new SFTP service to allow users access to several S3 buckets in an AWS account. In the past, we have used the AWS Transfer Family service to do this but left the default settings which created a publically available endpoint.</p><p>When creating a service straight out of the box, so to speak, it assigns a serverId to the endpoint with the remaining part of the endpoint as .server.transfer.&lt;regionId&gt;.amazonaws.com. This endpoint is publically available and protected by a public / private key combination and username.</p><p>The other two options available when creating the service are VPC and VPC Endpoint. We decided to use VPC Endpoint, although with a little more research the VPC option is also a good choice. Another article, maybe?</p><p><a href="https://github.com/MrC0ns1st3nt/AWS-Transfer-Service">MrC0ns1st3nt/AWS-Transfer-Service</a></p><p>The repo above contains the modules required to build out a working SFTP service with some minor tweaks, assuming you have a working VPC and S3 bucket.</p><p>The modules are written to provide the following:</p><ul><li>Single / Multiple users</li><li>Read-Only access (by default) unless specified</li><li>IAM policy created by variables entered</li><li>The creation of the public/private SSH keys</li></ul><p>Pull the code from the repo, make alterations as required and let me know if it was useful. The Terraform provided is working fine for my purpose at this time and although it isn’t perfect, it is useable.</p><p>Please note that using VPC Endpoint will only be available until March 31st 2021. After this time you will not be able to build new services with this feature.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db6858394c52" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Adding AWS Timestream to Prometheus.]]></title>
            <link>https://ellishoile.medium.com/adding-aws-timestream-to-prometheus-b26e5371e1bc?source=rss-13adabaa3685------2</link>
            <guid isPermaLink="false">https://medium.com/p/b26e5371e1bc</guid>
            <category><![CDATA[amazon-timescale]]></category>
            <category><![CDATA[prometheus]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Simon Bulmer]]></dc:creator>
            <pubDate>Thu, 26 Nov 2020 15:13:13 GMT</pubDate>
            <atom:updated>2020-11-26T15:13:13.717Z</atom:updated>
            <content:encoded><![CDATA[<p>In a continuation of my existing build (<a href="https://ellishoile.medium.com/prometheus-with-alert-manager-hosted-in-fargate-1e18e6dbb34f">Prometheus in Fargate</a>), I have been playing around with remote storage options and for the purposes of this document, I am using AWS Timestream.</p><p>Prometheus out of the box will store up to 15 days of metrics, but these are lost should the instance/server be terminated. With this in mind, it is worthwhile looking into storage options for metric data with a few options being:</p><ul><li>InfluxDB</li><li>TimescaleDB</li><li><a href="https://aws.amazon.com/timestream/?nc=sn&amp;loc=1">Amazon Timestream</a></li></ul><p>I am sure there are other options available but these are the most common I found when researching. For the sake of my sanity and the many rewrites I have already carried out for this document I am not going into detail regarding any of the above options, and simply showing how I built and updated my current Prometheus build to use the Amazon option.</p><p>The Amazon Timestream option requires the use of an adapter which can be configured in Prometheus as a remote_write option to store data. This adapter only uses write and as I am using Grafana for my dashboards I’ll need to install another plugin/adapter that allows connection to my Timestream DB.</p><h3>Creating the ECR repository</h3><p>Before starting you need to add an additional repository to the ECR and for this I have kept it simple and descriptive by calling it <strong>timestream-adapter</strong>.</p><p>This is where the build will be stored and called by the task_definition.json file in the Prometheus module.</p><h3>Cloning and building the Timestream adapter</h3><p>Using the existing build the folder structure is as follows:</p><pre>Prometheus<br>   Docker<br>      alertmanager<br>      prometheus</pre><p>We will create a new folder called timestream</p><pre>Prometheus<br>   Docker<br>      alertmanager<br>      prometheus<br>      timestream</pre><p>Navigate to this folder and clone the adapter created by <a href="https://github.com/dpattmann/prometheus-timestream-adapter">Dennis Pattmann</a> :</p><pre>git clone <a href="https://github.com/dpattmann/prometheus-timestream-adapter.git">https://github.com/dpattmann/prometheus-timestream-adapter.git</a></pre><p>Once cloned you need to prepare two of the files to suit your needs. The files are <strong>main.go</strong> and the <strong>Dockerfile</strong>. I had a few issues when first building this and actually reached out to Dennis in order to discuss. The Dockerfile you clone is actually incorrect and although it does build successfully, I did receive x509 errors in the Cloudwatch logs.</p><p>The <strong>Dockerfile</strong> when complete will look like:</p><pre>FROM golang:1.15.3-alpine3.12 as build</pre><pre>WORKDIR /src/prometheus-timestream-adapter</pre><pre>ADD . /src/prometheus-timestream-adapter</pre><pre>RUN apk add build-base ca-certificates</pre><pre>RUN go test</pre><pre>RUN CGO_ENABLED=0 GOOS=linux go build -o /prometheus-timestream-adapter</pre><pre>FROM scratch</pre><pre>COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/</pre><pre>COPY --from=build /prometheus-timestream-adapter /usr/local/bin/prometheus-timestream-adapter</pre><pre>ENTRYPOINT [&quot;/usr/local/bin/prometheus-timestream-adapter&quot;]</pre><p>and <strong>main.go</strong> has a few configurable under func init()</p><pre>func init() {</pre><pre>prometheus.MustRegister(receivedSamples)</pre><pre>prometheus.MustRegister(sentSamples)</pre><pre>prometheus.MustRegister(failedSamples)</pre><pre>prometheus.MustRegister(sentBatchDuration)</pre><pre>flag.StringVar(&amp;cfg.awsRegion, &quot;awsRegion&quot;, &quot;<strong>&lt;enter region&gt;</strong>&quot;, &quot;&quot;)</pre><pre>flag.StringVar(&amp;cfg.databaseName, &quot;databaseName&quot;, &quot;prometheus&quot;, &quot;&quot;)</pre><pre>flag.StringVar(&amp;cfg.listenAddr, &quot;listenAddr&quot;, &quot;:9201&quot;, &quot;&quot;)</pre><pre>flag.StringVar(&amp;cfg.tableName, &quot;tableName&quot;, &quot;prometheus-table&quot;, &quot;&quot;)</pre><pre>flag.StringVar(&amp;cfg.telemetryPath, &quot;telemetryPath&quot;, &quot;/metric&quot;, &quot;&quot;)</pre><pre>flag.StringVar(&amp;cfg.tlsCert, &quot;tlsCert&quot;, &quot;tls.cert&quot;, &quot;&quot;)</pre><pre>flag.StringVar(&amp;cfg.tlsKey, &quot;tlsKey&quot;, &quot;tls.key&quot;, &quot;&quot;)</pre><pre>flag.BoolVar(&amp;cfg.tls, &quot;tls&quot;, false, &quot;&quot;)</pre><pre>flag.Parse()</pre><pre>}</pre><p>Once these have been updated you are ready to build.</p><pre>docker build -t timestream-adapter .</pre><p>Once built you can push this to the waiting repository in AWS.</p><p>Obtain an authentication token and authenticate Docker:</p><pre>aws-runas <strong>&lt;name of account&gt;</strong> aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <strong>&lt;accountID&gt;</strong>.dkr.ecr.us-east-1.amazonaws.com</pre><p>Tag the image appropriately:</p><pre>docker tag timestream-adapter:latest <strong>&lt;accountID&gt;</strong>.dkr.ecr.us-east-1.amazonaws.com/timestream-adapter:latest</pre><p>Push the build to ECR:</p><pre>docker push <strong>&lt;accountID&gt;</strong>.dkr.ecr.us-east-1.amazonaws.com/timestream-adapter:latest</pre><h3>Amazon Timestream</h3><p>As stated above the current version of Terraform does not allow me to set up a Timestream database and table, so this has been completed manually using the AWS console.</p><p>Log in to the console and choose <strong>Amazon Timestream </strong>from the list of AWS services under Database (search if easier).</p><p>Create a database called <strong>prometheus</strong> by clicking the top right hand <strong>create database</strong> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*yA8CbGrZRQsLztrUGZaXqQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*8FGfDV1hkF8ZYP0ZRyckTA.png" /></figure><p>Once created click on the database called <strong>prometheus, </strong>click the <strong>tables</strong> tab and then click <strong>create table</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*wFCYH9Lng9qUiYFMS3OuNw.png" /></figure><p>I called my table <strong>prometheus-table, </strong>configuring<strong> </strong>the Memory store retention to 12 hours and the Magnetic store retention to 1 year.</p><h3><strong>Updating IAM</strong> permissions</h3><p>Pretty much the last thing I did was add permissions allowing the ECS task the ability to talk to Timestream. I wanted to follow the Cloudwatch logs to ensure it was working correctly before altering permissions and was relieved to see the ‘Access denied’ reference.</p><p>A reminder of the folder structure so far (including previous build):</p><pre>prometheus<br>   infrastructure<br>   docker<br>      alertmanager<br>      prometheus<br>      timestream<br>   modules<br>      ecs<br>      security-groups<br>      app<br>         templates</pre><p>For the purposes of this document though I am adding here. Update the ECS module file called <strong>main.tf </strong>to include the timestream policy. In this build, I am using the AWS policy called <strong>AmazonTimestreamFullAccess </strong>but would suggest practising the ‘least privilege’ method for your own builds.</p><p>ecs/main.tf</p><pre>resource &quot;aws_iam_role_policy_attachment&quot; &quot;timestream-access&quot; {<br>  role         = &quot;${aws_iam_role.task-definition-role.name}&quot;<br>  policy_arn   = &quot;arn:aws:iam::aws:policy/AmazonTimestreamFullAccess&quot;</pre><p>I added the above under the last entry for role_policy_attachment.</p><h3>Adding an additional CloudWatch log group</h3><p>Update <strong>main.tf</strong> with a new entry for aws_cloudwatch_log_group. This will be used by the task definition later on in this document.</p><p>ecs/main.tf</p><pre>resource &quot;aws_cloudwatch_log_group&quot; &quot;timestream&quot; {<br>  name          = &quot;/${var.env[&quot;environment&quot;]}/timestream&quot;<br>  retention_in_days = &quot;7&quot;<br>  tags = &quot;${merge(map(&quot;Name&quot;,format(&quot;%s-cloudwatch&quot;,var.tags[&quot;environment&quot;])), var.tags)}&quot;<br>}</pre><h3>Updating the Task Definition</h3><p>Navigate to modules/app/templates and open up the task_definition.json file. For this, we are going to add a new entry after the coveo/ecs-exporter section.</p><pre>{<br>     &quot;essential&quot;: false,<br>     &quot;cpu&quot;: 10,<br>     &quot;image&quot;: &quot;coveo/ecs-exporter&quot;,<br>     &quot;memory&quot;: 64,<br>     &quot;name&quot;: &quot;ecs-exporter&quot;,<br>     &quot;networkMode&quot;: &quot;awsvpc&quot;,<br>     &quot;command&quot;: [&quot;-aws.region=<strong>&lt;enter region&gt;</strong>&quot;],<br>     &quot;portMappings&quot;: [<br>       {<br>         &quot;containerport&quot;: 9222,<br>         &quot;hostPort&quot;: 9222,<br>         &quot;protocol&quot;: &quot;tcp&quot;<br>       }<br>      ]<br>     },</pre><p>Don’t forget the comma ^^^ at the end.</p><pre>{</pre><pre>&quot;essential&quot;: false,<br>&quot;cpu&quot;: 10,<br>&quot;image&quot;: &quot;<strong>&lt;accountID&gt;</strong>.dkr.ecr.<strong>&lt;enter region&gt;</strong>.amazonaws.com/timestream-adapter:latest&quot;,<br>&quot;memoryReservation&quot;: 128,<br>&quot;name&quot;: &quot;timestream-adapter&quot;,<br>&quot;networkMode&quot;: &quot;awsvpc&quot;,<br>&quot;portMappings&quot;: [<br>  {<br>   &quot;containerPort&quot;: 9201,<br>   &quot;hostPort&quot;: 9201,<br>   &quot;protocol&quot;: &quot;tcp&quot;<br> }<br>],<br>&quot;logConfiguration&quot;: {<br>  &quot;logDriver&quot;: &quot;awslogs&quot;,<br>  &quot;options&quot;: {<br>    &quot;awslogs-group&quot;: &quot;/${awslogs-group}/timestream&quot;,<br>    &quot;awslogs-region&quot;: &quot;${awslogs-region}&quot;,<br>    &quot;awslogs-stream-prefix&quot;: &quot;timestream&quot;<br>   }<br>  }<br> }<br>]</pre><p>The new entry will more than likely change in the future as I convert to variables etc. For the purposes of this build, we can manage with the above.</p><h3>Target Groups and Listener update</h3><p>The last thing we need to do with the infrastructure is to update the target group to include port 9201. Navigate to modules/app and open <strong>main.tf.</strong></p><p>Add the following after “aws_alb_target_group” “alertmanager”:</p><pre># Adding target group for timestream adapter</pre><pre>resource &quot;aws_alb_target_group&quot; &quot;timestream&quot; {<br>name                      = &quot;${var.env[&quot;environment&quot;]}-ts&quot;<br>port                      = 9201<br>protocol                  = &quot;HTTP&quot;<br>target_type               = &quot;ip&quot;<br>vpc_id                    = &quot;${var.vpc_id}&quot;<br>health_check {<br>  matcher = &quot;200,302&quot;<br> }<br>}</pre><pre>resource &quot;aws_alb_listener&quot; &quot;timestream&quot; {<br>load_balancer_arn = &quot;${aws_alb.main.id}&quot;<br>port              = 9201<br>protocol          = &quot;HTTP&quot;</pre><pre>default_action {<br>  target_group_arn = &quot;${aws_alb_target_group.timestream.id}&quot;<br>  type             = &quot;forward&quot;<br>  }<br>}</pre><h3>Adding remote_write</h3><p>In order for Prometheus to use the timestream-adapter you need to add a reference to it in the prometheus.yml file and rebuild the image. Navigate to prometheus/docker/prometheus and open <strong>prometheus.yml.</strong></p><p>Add the following :</p><pre>remote_write:<br>  - url: &quot;http://localhost:9201/write&quot;</pre><p>Rebuild the image as before:</p><pre>docker build -t prometheus .</pre><p>Tag the image:</p><pre>docker tag prometheus:latest &lt;<strong>accountID</strong>&gt;.dkr.ecr.<strong>&lt;enter region&gt;</strong>.amazonaws.com/prometheus:latest</pre><p>Push the image to ECR:</p><pre>docker push &lt;<strong>accountID</strong>&gt;.dkr.ecr.<strong>&lt;enter region&gt;</strong>.amazonaws.com/prometheus:latest</pre><h3>And finally</h3><p>At this point, you should be ready to execute a Terraform apply. In my environment, I stopped the prometheus task in order to force it to restart a new task with the changes, and after a few minutes I could not only see logs for the timestream task but data in the Timestream DB created.</p><p>I have gone over this document should have captured all the steps I took in order to build out the Timestream DB in AWS and utilize the adapter written by Dennis in Go. If however, you do notice something I have missed or want to give me any feedback, please leave me a comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b26e5371e1bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prometheus with Alert Manager hosted in Fargate.]]></title>
            <link>https://ellishoile.medium.com/prometheus-with-alert-manager-hosted-in-fargate-1e18e6dbb34f?source=rss-13adabaa3685------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e18e6dbb34f</guid>
            <category><![CDATA[fargate]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[prometheus]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[ec]]></category>
            <dc:creator><![CDATA[Simon Bulmer]]></dc:creator>
            <pubDate>Mon, 15 Jun 2020 13:38:29 GMT</pubDate>
            <atom:updated>2020-11-26T10:50:52.577Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTRLB2NHMa7fKsVs-C0BEQ.jpeg" /></figure><p>Hopefully, after reading this article you should be able to setup Prometheus in a Docker container using Node Exporter, ECS exporter to scrape metrics.</p><p>In an attempt to do the above I made many mistakes and this is by no means a complete guide, only my experience in building a working Prometheus environment hosted using AWS Fargate.</p><h3>Creating the Docker images</h3><p>For this build, I created two separate images and pushed them to two separate ECRs. The assumption is that you have already created the two repositories in AWS Elastic Container Registry called alertmanager and prometheus for the images to stored.</p><p>Create a working folder called Prometheus, followed by a subfolder called Docker. A further two folders will need to be created called prometheus and alertmanager.</p><p>In the alertmanager folder you will need to create a file called Dockerfile with the contents:</p><pre>FROM prom/alertmanager:latest</pre><pre>ADD alertmanager.yml /etc/alertmanager/</pre><p>I also created a very simple route listening for an alert rule called ExportDown in a file called alertmanager.yml:</p><pre>global:<br>  resolve_timeout: 5m</pre><pre>route: <br>  group_by: [&#39;instance&#39;,&#39;severity&#39;]<br>  routes:<br>    - match:<br>        alertname: ExporterDown<br>  receiver: &#39;pushover&#39;<br>receivers:<br>- name: &#39;pushover&#39;</pre><p>Please note that at the time of writing this the alerting is still in its infancy and a work in progress. I do intend on linking to PagerDuty but need to ensure all required metrics are being scraped before continuing.</p><p>In the empty prometheus folder you will again create a file called Dockerfile with the contents:</p><pre>FROM prom/prometheus:v2.14.0</pre><pre>ADD prometheus.yml /etc/prometheus/<br>ADD irst_rules.yml /etc/prometheus/<br>ADD targets/*.yml /etc/prometheus/targets/*.yml</pre><p>a file called prometheus.yml:</p><pre>global:<br>  scrape_interval:   5s<br>  evaluation_interval:   10s</pre><pre>rule_files:<br>  - &quot;first_rules.yml&quot;</pre><pre>scrape_configs:<br>  - job_name: prometheus<br>    static_configs:<br>      - targets: [&#39;localhost:9090&#39;]<br>  - job_name: node-exporter&#39;<br>    ec2_sd_configs:<br>      - port: 9100<br>        region: &#39;us-east-1&#39;<br>        profile: &#39;<strong>&lt;enter profile name&gt;</strong>&#39;<br>    relabel_configs:<br>      - source_labels: [__meta_ec2_tag_service_name]<br>        action: keep<br>        regex: &#39;<strong>&lt;enter regex string&gt;</strong>&#39;<br>      - source_labels: [__meta_ec2_tag_name]<br>        target_label: instance<br>      - source_labels: [__meta_ec2_instance_id]<br>        target_label: instance_id<br>  - job_name: ecs-exporter<br>    static_configs: <br>      - targets: [&#39;localhost:9222&#39;]<br>  - job_name: &#39;jmx-exporter&#39;<br>    ec2_sd_configs:<br>      - port: 9404<br>        region: &#39;us-east-1&#39;<br>        profile: &#39;<strong>&lt;enter profile name&gt;</strong>&#39;<br>    relabel_configs:<br>      - source_labels: [__meta_ec2_tag_service_name]<br>        action: keep<br>        regex: &#39;<strong>&lt;enter regex string&gt;</strong>&#39;<br>      - source_labels: [__meta_ec2_tag_name]<br>        target_label: name<br>      - source_labels: [__meta_ec2_instance_id]<br>        target_label: instance_id</pre><pre>alerting:<br>  alertmanagers:<br>    - static_configs:<br>      - targets: [&#39;localhost:9093&#39;]</pre><p>Breaking away from the above for just a second you will have noticed I have highlighted a few of the entries. These entries are specific to the AWS profile I have set on my system and the environment I am scraping metrics from. I use a written application called aws-runas when using AWS due to the number of accounts I switch between.</p><p><a href="https://github.com/mmmorris1975/aws-runas">mmmorris1975/aws-runas</a></p><p>Create a file called first_rules.yml. As you can see it is referenced in the above prometheus.yml file. I currently have 5 rules set in this file:</p><pre>groups:<br>  - name: alert<br>    rules:<br>    - alert: PrometheusNotConnectedToAlertManager<br>      expr: prometheus_notifications_alertmanagers_discovered &lt; 1<br>      for: 5m<br>      labels:<br>        severity: error<br>      annotations:<br>        summary: &quot;Prometheus is not connected to an alertmanager (instance {{ $labels.instance }})&quot;<br>        description: &quot;Prometheus cannot connect the alertmanager\n VALUE = {{ $value }}\n LABELS: {{ $labels }}&quot;<br>    - alert: ExporterDown<br>      expr: up{job=&quot;node&quot;} == 0 <br>      for: 3m<br>      labels:<br>        severity: warning<br>      annotations:<br>        summary: &quot;Exporter down ( instance {{ $labels.instance }})&quot;<br>        description: &quot;Prometheus exporter down\n VALUE = {{ $value }}\n LABELS: {{ $labels }}&quot;<br>    - alert: OutOfMemory<br>      expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100 &lt; 10<br>      for: 5m<br>      labels:<br>        severity: warning<br>      annotations:<br>        summary: &quot;Out of memory ( instance {{ $labels.instance }})&quot;<br>        description: &quot;Node memory is filling up (&lt; 10% left)\n VALUE = {{ $value }}\n LABELS: {{ $labels }}&quot;<br>    - alert: OutOfDiskSpace<br>      expr: (node_filesystem_avail_bytes{mountpoint=&quot;/rootfs&quot;} * 100) / node_filesystem_size_bytes{mountpoint=&quot;/rootfs&quot;} &lt; 10<br>      for: 5m<br>      labels:<br>        severity: warning<br>      annotations:<br>        summary: &quot;Out of disk space (instance {{ $labels.instance }})&quot;<br>        description: &quot;Disk is almost full (&lt; 10% left)\n VALUE = {{ $value }}\n LABELS: {{ $labels }}&quot;<br>    - alert: HighCpuLoad<br>      expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode=&quot;idle&quot;}[5m])) * 100) &gt; 80<br>      for: 5m<br>      labels:<br>        severity: warning<br>      annotations:<br>        summary: &quot;High CPU load (instance {{ $labels.instance }})&quot;<br>        description: &quot;CPU load is &gt; 80%\n VALUE = {{ $value }}\n LABELS: {{ $labels }}&quot;</pre><p>And that is pretty much it for the image creation. We should build and push the two images to Elastic Container Registry (ECR).</p><h3><strong>Building and pushing to Elastic Container Registry</strong></h3><p>The folder structure should look like the following:</p><pre>Prometheus<br>   Docker<br>      alertmanager<br>      prometheus</pre><p>Before you carry out a Docker Pull / Push from ECR in AWS you need to grab the credentials for authentication. I use the following:</p><pre>aws-runas &lt;name of account&gt; aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin &lt;accountID&gt;.dkr.ecr.us-east-1.amazonaws.com</pre><p>Where the <strong>account profile </strong>on my system is contained within the ~/.aws/config file and name accordingly.</p><p>In order to build the alertmanager image you should navigate to Prometheus →Docker →alertmanager then:</p><pre>docker build -t alertmanager .</pre><p>Tag the image accordingly:</p><pre>docker tag alertmanager:latest &lt;<strong>account</strong>&gt;.dkr.ecr.&lt;<strong>region</strong>&gt;.amazonaws.com/alertmanager:latest</pre><p>where the <strong>account</strong> is your AWS account number and <strong>region</strong> is the region you are using.</p><p>And finally, push the tagged image to the repository:</p><pre>docker push &lt;<strong>account</strong>&gt;.dkr.ecr.&lt;<strong>region</strong>&gt;.amazonaws.com/alertmanager:latest</pre><p>Navigate to Prometheus → Docker → prometheus and do the same as above:</p><pre>docker build -t prometheus .<br>docker tag prometheus:latest &lt;<strong>account</strong>&gt;.dkr.ecr.&lt;<strong>region</strong>&gt;.amazonaws.com/prometheus:latest<br>docker push &lt;<strong>account</strong>&gt;.dkr.ecr.&lt;<strong>region</strong>&gt;.amazonaws.com/prometheus:latest</pre><p>The above two images should be present in their respective repositories within ECR and ready to use.</p><h3>Creating the ECS module</h3><p>So far we have created the images and pushed them to the Elastic Container Registry in an AWS account. We now need to move onto the module creation and then finally the deployment. All Terraform code mentioned in this article is using version 0.12.9.</p><p>To keep thing neat you should navigate back to your Prometheus folder and create a new folder called Modules. Within Modules create another folder called ECS. Once inside the ECS folder, you should create main.tf containing the following:</p><pre>/*==== IAM roles =====*/</pre><pre>resource &quot;aws_iam_role&quot; &quot;ecs-service-role&quot; {<br>  name                = &quot;${var.tags[&quot;environment&quot;]}-ecs-service-role&quot;</pre><pre>  assume_role_policy  = &lt;&lt;EOF<br>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>      {<br>        &quot;Action&quot;: &quot;sts:AssumeRole&quot;,<br>        &quot;Principal&quot;: {<br>          &quot;Service&quot;: &quot;ecs.amazonaws.com&quot;<br>        },<br>        &quot;Effect&quot;: &quot;Allow&quot;,<br>        &quot;Sid&quot;: &quot;&quot;<br>      }<br>    ]<br>  }<br>EOF<br>}</pre><pre>resource &quot;aws_iam_role&quot; &quot;ecs-instance-role&quot; {<br>    name               = &quot;${var.tags[&quot;environment&quot;]}-ecs-instance-role&quot;<br>    path               = &quot;/&quot;<br>    assume_role_policy = &quot;${data.aws_iam_policy_document.ecs-instance-policy.json}&quot;<br>}</pre><pre>data &quot;aws_iam_policy_document&quot; &quot;ecs-instance-policy&quot; {<br>    statement {<br>        actions = [&quot;sts:AssumeRole&quot;]</pre><pre>        principals {<br>           type        = &quot;Service&quot;<br>           identifiers = [&quot;ec2.amazonaws.com&quot;]<br>        }<br>    }<br>}</pre><pre>resource &quot;aws_iam_role_policy_attachment&quot; &quot;ecs-instance-role-attachment&quot; {<br>    role        = &quot;${aws_iam_role.ecs-instance-role.name}&quot;<br>    policy_arn  = arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role&quot;<br>}</pre><pre>resource &quot;aws_iam_instance_profile&quot; &quot;ecs-instance-profile&quot; {<br>    name = &quot;${var.env[&quot;environment&quot;]}-ecs-instance-profile&quot;<br>    path = &quot;/&quot;<br>    role = &quot;${aws_iam_role.ecs-instance-role.id}&quot;<br>    provisioner &quot;local-exec&quot; {<br>      command = &quot;sleep 10&quot;<br>    }<br>}</pre><pre>/*==== Create IAM Task Definition Role =====*/</pre><pre>data &quot;aws_iam_policy_document&quot; &quot;ecs-service-policy&quot; {<br> statement {<br>   actions           = [&quot;sts:AssumeRole&quot;}</pre><pre>   principals {<br>     type            = &quot;Service&quot;<br>     identifiers     = [&quot;ecs-tasks.amazonaws.com&quot;]<br>   }<br>  }<br>}</pre><pre>resource &quot;aws_iam_policy&quot; &quot;policy&quot; {<br>  name               = &quot;${var.env[&quot;environment&quot;]}-ecs-exporter&quot;<br>  policy  = &lt;&lt;EOF<br>{</pre><pre>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Sid&quot;: &quot;&quot;,<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [ <br>                &quot;ecs:ListServices&quot;,<br>                &quot;ecs:ListContainerInstances&quot;,<br>                &quot;ecs:ListClusters&quot;,<br>                &quot;ecs:DescribeServices&quot;,<br>                &quot;ecs:DescribeContainerInstances&quot;,<br>                &quot;ecs:DescribeClusters&quot;<br>            ],<br>            &quot;Resource&quot;: &quot;*&quot;<br>        }<br>    ]<br>}<br>EOF<br>}</pre><pre>resource &quot;aws_iam_role&quot; &quot;task-definition-role&quot; {<br>  name               = &quot;${var.env{&quot;environment&quot;]}-task-definition&quot;<br>  assume_role_policy = &quot;${data.aws_iam_policy_document.ecs-service-policy.json}&quot;<br>}</pre><pre>resource &quot;aws_iam_role_policy_attachment&quot; &quot;ecs-exporter&quot; {<br>  role               = &quot;${aws_iam_role.task-definition-role.name}&quot;<br>  policy_arn         = &quot;${aws_iam_policy.policy.arn}&quot;<br>}</pre><pre>resource &quot;aws_iam_role_policy_attachment&quot; &quot;ecs-service-role-attachment&quot; {<br>  role               = &quot;${aws_iam_role.task-definition-role.name}&quot;<br>  policy_arn         = &quot;arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy&quot;<br>}</pre><pre>resource &quot;aws_iam_role_policy_attachment&quot; &quot;prometheus-read-access&quot; {<br>  role               = &quot;${aws_iam_role.task-definition-role.name}&quot;<br>  policy_arn         = &quot;arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess&quot;<br>}</pre><pre>/*==== ECS Fargate Cluster =====*/</pre><pre>resource &quot;aws_ecs_cluster&quot; &quot;main&quot; {<br>  name            = &quot;${var.env[&quot;environment&quot;]}&quot;<br>}</pre><pre>/*==== CloudWatch =====*/</pre><pre>resource &quot;aws_cloudwatch_log_group&quot; &quot;logs&quot;<br>  name              = &quot;/${var.env[&quot;environment&quot;]}/prometheus&quot;<br>  retention_in_days = &quot;7&quot;<br>  tags              = &quot;${merge(map(&quot;Name&quot;,format(&quot;%s-cloudwatch&quot;,var.tags[&quot;environment&quot;])),var.tags)}&quot;<br>}</pre><pre>resource &quot;aws_cloudwatch_log_group&quot; &quot;alertmanager&quot; {<br>  name              = &quot;/${var.env[&quot;environment&quot;]}/alertmanager&quot;<br>  retention_in_days = &quot;7&quot;<br>  tags              = &quot;${merge(map(&quot;Name&quot;,format(&quot;%s-cloudwatch&quot;,var.tags[&quot;environment&quot;])),var.tags)}&quot;<br>} </pre><p>Create another file called outputs.tf:</p><pre>output &quot;ecs_cluster&quot; {<br>    value = &quot;${aws_ecs_cluster.main.name}&quot;<br>}<br>output &quot;cluster_id&quot; {<br>    value = &quot;${aws_ecs_cluster.main.id}&quot;<br>}</pre><pre>output &quot;task_definition_role_arn&quot; {<br>    value = &quot;${aws_iam_role.task-definition-role.arn}&quot;<br>}</pre><p>And finally, create a file called vars.tf:</p><pre>variable &quot;env&quot; {type = &quot;map&quot;}<br>variable &quot;tags&quot; {type = &quot;map&quot;}</pre><pre>variable &quot;vpc_id&quot; {}</pre><h3>Creating the Security Group module</h3><p>Move up a level in the folder structure and create a folder called security-groups. Create a file called main.tf with the following contents:</p><pre>resource &quot;aws_security_group&quot; &quot;alb-security-group&quot; {<br>  name        = &quot;${var.env[&quot;environment&quot;]}-alb&quot;<br>  description = &quot;ALB security group for environment ${var.tags[&quot;environment&quot;]}&quot;<br>  vpc_id      = &quot;${var.vpc_id}&quot;</pre><pre>  ingress {<br>    from_port   = 9090<br>    to_port     = 9090<br>    protocol    = &quot;TCP&quot;<br>    cidr_blocks = [&quot;&lt;enter your cidr block&gt;&quot;]<br>    description = &quot;Prometheus access&quot;<br>}</pre><pre>  ingress {<br>    from_port   = 9093<br>    to_port     = 9093<br>    protocol    = &quot;TCP&quot;<br>    cidr_blocks = [&quot;&lt;enter your cidr block&gt;&quot;]<br>    description = &quot;AlertManager access&quot;<br>}</pre><pre>  egress {<br>    from_port   = 0<br>    to_port     = 0<br>    protocol    = &quot;-1&quot;<br>    cidr_blocks = [&quot;0.0.0.0/0&quot;]<br>}</pre><pre>lifecycle {<br>  create_before_destroy = true<br>}</pre><pre>tags = &quot;${merge(map(&quot;Name&quot;,format(&quot;%s-alb&quot;,var.tags[&quot;environment&quot;])),var.tags,<br>map(&quot;service-name&quot;,format(&quot;%s-security-group&quot;,var.tags[&quot;environment&quot;])),<br>map(&quot;service-type&quot;,&quot;security-group&quot;))}&quot;<br>}</pre><pre>resource &quot;aws_security_group&quot; &quot;worker-security-group&quot; {<br>   name        = &quot;${var.env[&quot;environment&quot;]}-worker&quot;<br>   description = &quot;Worker security group for environment ${var.tags[&quot;environment&quot;]}&quot;<br>   vpc_id      = &quot;${var.vpc_id}&quot;</pre><pre>ingress {<br>  from_port = 9090<br>  to_port   = 9090<br>  protocol  = &quot;TCP&quot;<br>  security_groups = [&quot;${aws_security_group.alb-security-group.arn}&quot;]<br>  description = &quot;Load balancer to Prometheus&quot;<br>}</pre><pre>ingress {<br>  from_port = 9093<br>  to_port   = 9093<br>  protocol  = &quot;TCP&quot;<br>  security_groups = [&quot;${aws_security_group.alb-security-group.arn}&quot;]<br>  description = &quot;Load balancer to AlertManager&quot;<br>}</pre><pre>egress {<br>  from_port = 0<br>  to_port   = 0<br>  protocol  = &quot;-1&quot;<br>  cidr_blocks = [&quot;0.0.0.0/0&quot;]<br>}</pre><pre>lifecycle {<br>  create_before_destroy = true<br>}</pre><pre>tags = &quot;${merge(map(&quot;Name&quot;,format(&quot;%s-worker&quot;,var.tags[&quot;environment&quot;])),var.tags,<br>map(&quot;service-name&quot;,format(&quot;%s-security-group&quot;,var.tags[&quot;environment&quot;])),<br>map(&quot;service-type&quot;,&quot;security-group&quot;))}&quot;<br>}</pre><p>Create another file called outputs.tf:</p><pre>output &quot;alb-security-group&quot; { value = &quot;${aws_security_group.alb-security-group.name}&quot; }</pre><pre>output &quot;alb-security-group-id&quot; { value = &quot;${aws_security_group.alb-security-group.id}&quot; }</pre><pre>output &quot;worker-security-group&quot; { value = &quot;${aws_security_group.worker-security-group.name}&quot; }</pre><pre>output &quot;worker-security-group-id { value = &quot;${aws_security_group.worker-security-group.id}&quot; }</pre><p>And finally, create a file called vars.tf:</p><pre>variable &quot;vpc_id&quot; {}</pre><pre>variable &quot;tags&quot; { type = &quot;map&quot;}</pre><pre>variable &quot;env&quot; { type = &quot;map&quot;}</pre><h3>Creating the application module</h3><p>This is the final module required by the build containing the Terraform files with an additional template subfolder for the task definition. In the modules folder, you should create app, then a subfolder called templates. At this stage your folder structure should pretty much look like this:</p><pre>prometheus<br>   docker<br>      alertmanager<br>      prometheus<br>   modules<br>      ecs<br>      security-groups<br>      app<br>         templates</pre><p>Starting with the app folder, create a file called main.tf:</p><pre>data &quot;aws_subnet_ids&quot; &quot;private&quot; {<br>    vpc_id      = &quot;${var.vpc_id}&quot;</pre><pre>    tags = {<br>        network = &quot;private&quot;<br>    }<br>}</pre><pre>data &quot;aws+subnet_ids&quot; &quot;public&quot; {<br>    vpc_id      = &quot;${var.vpc_id}&quot;</pre><pre>    tags = {<br>        network = &quot;public&quot;<br>    }<br>}</pre><pre>/*==== Create ECS Task Definition =====*/</pre><pre>data &quot;template_file&quot; &quot;container_definition&quot; {<br>    template           = &quot;${file(&quot;${path.module}/templates/task_definition.json&quot;)}&quot;</pre><pre>    vars = {<br>       app_name        = local.app.app_name<br>       name            = &quot;${var.env[&quot;environment&quot;]}-${local.app.app_name}&quot;<br>       prom_image      = local.app.prom_image<br>       alrt_image      = local.app.alrt_image<br>       app_cpu         = local.app.app_cpu<br>       app_memory      = local.app.app_memory<br>       awslogs-group   = &quot;${var.env[&quot;environment&quot;]}&quot;<br>       awslogs-region  = &quot;${var.env[&quot;region&quot;]}&quot;<br>    }<br>}</pre><pre>resource &quot;aws_ecs_task_definition&quot; &quot;app&quot; {<br>  family                   = &quot;${var.env[&quot;environment&quot;]}-${var.app[&quot;app_name&quot;]}&quot;<br>  network_mode             = &quot;${var.network_mode}&quot;<br>  requires_compatibilities = [&quot;FARGATE&quot;]<br>  cpu                      = &quot;${var.app[&quot;fargate_cpu&quot;]}&quot;<br>  memory                   = &quot;${var.app[&quot;fargate_memory&quot;]}&quot;<br>  execution_role_arn       = &quot;${var.task-definition-role-arn}&quot;<br>  task_role_arn            = &quot;${var.task-definition-role-arn}&quot;<br>  container_definitions    = &quot;${data.template_file.container_definition.rendered}&quot;<br>}</pre><pre>/*==== Create ECS Service =====*/</pre><pre>resource &quot;aws_ecs_service&quot; &quot;main&quot; {<br>  name                = &quot;${var.env[&quot;environment&quot;]}-${var.app[&quot;app_name&quot;]}&quot;<br>  cluster             = &quot;${var.ecs_cluster}&quot;<br>  task_definition     = &quot;${aws_ecs_task_definition.app.family}:${aws_ecs_task_definition.app.revision}&quot;<br>  desired_count       = &quot;${var.app_count}&quot;<br>  launch_type         = &quot;FARGATE&quot;</pre><pre>  network_configuration {<br>    security_groups   = [&quot;$var.worker-security-group-id}&quot;]<br>    subnets           = &quot;${data.aws_subnet_ids.private.ids}&quot;<br>  }</pre><pre>  load_balancer {<br>    target_group_arn  = &quot;${aws_alb_target_group.server.arn}&quot;<br>    container_name    = &quot;${var.env[&quot;environment&quot;]}-${var.app[&quot;app_name&quot;]}&quot;<br>    container_port    = 9090<br>  }</pre><pre>  load_balancer {<br>    target_group_arn  = &quot;${aws_alb_target_group.alertmanager.arn}&quot;<br>    container_name    = &quot;alertmanager&quot;<br>    container_port    = 9093<br>  }</pre><pre>  depends_on = [<br>    &quot;aws_alb_listener.server&quot;,<br>  ]<br>}</pre><pre>/*==== Create Route53 record =====*/</pre><pre>data &quot;aws_route53_zone&quot; &quot;hosted_zone&quot; {<br>  name = &quot;${var.hosted_zone}&quot;<br>}</pre><pre>resource &quot;aws_route53_record&quot; &quot;frontend_alb_r53&quot; {<br>  name       = &quot;${var.app[&quot;route53_name&quot;]}&quot;<br>  depends_on = [&quot;aws_alb.main&quot;]<br>  zone_id    = &quot;${data.aws_route53_zone.hosted_zone.zone_id}&quot;<br>  type       = &quot;A&quot;</pre><pre>  alias {<br>    name                   = &quot;${aws_alb.main.dns_name}&quot;<br>    zone_id                = &quot;${aws_alb.main.zone_id}&quot;<br>    evaluate_target_health = true<br>  }</pre><pre>  lifecycle {<br>    create_before_destroy = true<br>  }<br>}</pre><pre>/*==== Create Application Load Balancer =====*/</pre><pre>resource &quot;aws_alb&quot; &quot;main&quot; {<br>  name               = &quot;${var.env[&quot;environment&quot;]}-${var.app[&quot;app_name&quot;]}&quot;<br>  load_balancer_type = &quot;application&quot;<br>  subnets            = &quot;${data.aws_subnet_ids.public.ids}&quot;<br>  security_groups    = [&quot;$var.alb-security-group-id}&quot;]</pre><pre>  tags = &quot;${merge(map(&quot;Name&quot;, format(&quot;%s-${var.app[&quot;app_name&quot;]}-app-alb&quot;, var.tags[&quot;environment&quot;])),var.tags)}&quot;<br>}</pre><pre>resource &quot;aws_alb_target_group&quot; &quot;server&quot; {<br>  name        = &quot;${var.env[&quot;environment&quot;]}-${var.app[&quot;app_name&quot;]}&quot;<br>  port        = &quot;${var.app[&quot;app_port&quot;]}&quot;<br>  protocol    = &quot;HTTP&quot;<br>  target_type = &quot;ip&quot;<br>  vpc_id      = &quot;${var.vpc_id}&quot;<br>  health_check {<br>    matcher = &quot;200,302&quot;<br>  }<br>}</pre><pre>resource &quot;aws_alb_tagret_group&quot; &quot;alertmanager&quot; {<br>  name        = &quot;${var.env[&quot;environment&quot;]}-alrtmgr&quot;<br>  port        = 9093<br>  protocol    = &quot;HTTP&quot;<br>  target_type = &quot;ip&quot;<br>  vpc_id      = &quot;${var.vpc_id}&quot;<br>  health_check {<br>    matcher = &quot;200,302&quot;<br>  }<br>}</pre><pre>resource &quot;aws_alb_listener&quot; &quot;server&quot; {<br>  load_balancer_arn    = &quot;${aws_alb.main.id}&quot;<br>  port                 = &quot;${var.app[&quot;app_port&quot;]}&quot;<br>  protocol             = &quot;HTTP&quot;</pre><pre>  default_action {<br>    target_group_arn = &quot;${aws_alb_target_group.server.id}&quot;<br>    type             = &quot;forward&quot;<br>  }<br>}</pre><pre>resource &quot;aws_alb_listener&quot; &quot;node&quot; {<br>  load_balancer_arn = &quot;${aws_alb.main.id}&quot;<br>  port              = 9100<br>  protocol          = &quot;HTTP&quot;</pre><pre>  default_action {<br>    target_group_arn = &quot;${aws_alb_target_group.server.id}&quot;<br>    type = &quot;forward&quot;<br>  }<br>}</pre><pre>resource &quot;aws_alb_listener&quot; &quot;alertmanager&quot; {<br>  load_balancer_arn    = &quot;${aws_alb.main.id}&quot;<br>  port                 = 9093<br>  protocol             = &quot;HTTP&quot;</pre><pre>  default_action {<br>    target_group_arn = &quot;${aws_alb_target_group.alertmanager.id}&quot;<br>    type = &quot;forward&quot;<br>  }<br>}</pre><p>and finally the vars.tf:</p><pre>variable &quot;env&quot; { type = &quot;map&quot;}<br>variable &quot;tags&quot; { type = &quot;map&quot;}</pre><pre>variable &quot;vpc_id {}<br>variable &quot;app&quot; { type = &quot;map&quot; }</pre><pre>variable &quot;app_port&quot; {}<br>variable &quot;ecs_cluster&quot; {}</pre><pre>variable &quot;app_count&quot; {}</pre><pre>variable &quot;worker-security-group-id&quot; {}<br>variable &quot;alb-security-group-id {}</pre><pre>variable &quot;task-definition-role-arn {}</pre><pre>variable &quot;fargate_cpu&quot; {}<br>variable &quot;fargate_memory&quot; {}</pre><pre>variable &quot;network_mode&quot; {}<br>variable &quot;hosted_zone&quot; {}</pre><pre>locals {<br>    defaults = {<br>        ecr           = &quot;&quot;<br>        route53       = &quot;&quot;<br>        prom_image    = &quot;&lt;account&gt;.dkr.ecr.&lt;region&gt;.amazonaws.com/prometheus:latest&quot;<br>        alrt_image    = &quot;&lt;account&gt;.dkr.ecr.&lt;region&gt;.amazonaws.com/alertmanager:latest&quot;<br>        app_port      = 9090<br>        app_cpu       = 1024<br>        app_memory    = 2056<br>        hosted_zone   = &quot;&lt;enter hosted zone name&gt;&quot;<br>        network_mode  = &quot;awsvpc&quot;<br>        app_name      = &quot;server&quot;<br>    }<br>    app = merge(<br>        local.defaults,<br>        var.app<br>    )<br>}</pre><p>Moving onto the final piece for the app module. Next, we need to create the task definition file. In the template subfolder, you need to create a file called task_definition.json:</p><pre>[<br>   {<br>     &quot;essential&quot;: true,<br>     &quot;cpu&quot;: 128,<br>     &quot;image&quot;: &quot;${prom_image}&quot;,<br>     &quot;memory&quot;: 128,<br>     &quot;name&quot;: &quot;${name}&quot;<br>     &quot;networkMode&quot;: &quot;awsvpc&quot;,<br>     &quot;portMappings&quot;: [<br>       {<br>         &quot;containerPort&quot;: 9090,<br>         &quot;hostPort&quot;: 9090,<br>         &quot;protocol&quot;: &quot;tcp&quot;<br>       }<br>     ],<br>     &quot;logConfiguration&quot;: {<br>       &quot;logDriver&quot;: &quot;awslogs&quot;,<br>       &quot;options: {<br>         &quot;awslogs-group&quot;: &quot;/${awslogs-group}/prometheus&quot;,<br>         &quot;awslogs-region&quot;: &quot;${awslogs-region}&quot;,<br>         &quot;awslogs-stream-prefix&quot;: &quot;prometheus&quot;<br>       }<br>     }<br>   ],<br>   {<br>     &quot;essential&quot;: true,<br>     &quot;cpu&quot;: 128,<br>     &quot;image&quot;: &quot;${alrt_image}&quot;<br>     &quot;memory&quot;: 64,<br>     &quot;name&quot;: &quot;alertmanager&quot;,<br>     &quot;networkMode&quot;: &quot;awsvpc&quot;,<br>     &quot;portMappings&quot;: [<br>       {<br>         &quot;containerPort&quot;: 9093,<br>         &quot;hostPort&quot;: 9093,<br>         &quot;protocol&quot;: &quot;tcp&quot;<br>       }<br>     ],<br>     &quot;logConfiguration&quot;: {<br>       &quot;logDriver&quot;: &quot;awslogs&quot;,<br>       &quot;options&quot;: {<br>         &quot;awslogs-group&quot;: &quot;/${awslogs-group}/alertmanager&quot;,<br>         &quot;awslogs-region&quot;: &quot;${awslogs-region}&quot;,<br>         &quot;awslogs-stream-prefix&quot;: &quot;alertmanager&quot;<br>       }<br>     }<br>   ],<br>   {<br>     &quot;essential&quot;: false,<br>     &quot;cpu&quot;: 10,<br>     &quot;image&quot;: &quot;coveo/ecs-exporter&quot;,<br>     &quot;memory&quot;: 64,<br>     &quot;name&quot;: &quot;ecs-exporter&quot;,<br>     &quot;networkMode&quot;: &quot;awsvpc&quot;,<br>     &quot;command&quot;: [&quot;-aws.region=<strong>&lt;enter region&gt;</strong>&quot;],<br>     &quot;portMappings&quot;: [<br>       {<br>         &quot;containerport&quot;: 9222,<br>         &quot;hostPort&quot;: 9222,<br>         &quot;protocol&quot;: &quot;tcp&quot;<br>       }<br>      ]<br>     }<br>]</pre><h3>Bring the build together</h3><p>The app module is now completed and at the moment uses a pre-built exporter image from <a href="https://hub.docker.com/r/coveo/ecs-exporter">https://hub.docker.com/r/coveo/ecs-exporter</a>. The last part of the Prometheus deployment brings all the modules together for deployment. Create a folder called infrastructure in the top-level prometheus location with the final structure looking like this:</p><pre>prometheus<br>   infrastructure<br>   docker<br>      alertmanager<br>      prometheus<br>   modules<br>      ecs<br>      security-groups<br>      app<br>         templates</pre><p>Create a file called main.tf:</p><pre>module &quot;security-groups&quot; {<br>  source           = &quot;../modules/security-groups&quot;<br>  env              = &quot;${var.env}&quot;<br>  tags             = &quot;${var.tags}&quot;<br>  vpc_id           = &quot;${var.vpc_id}&quot;<br>}</pre><pre>module &quot;ecs&quot; {<br>  source           = &quot;../modules/ECS&quot;<br>  env              = &quot;${var.env}&quot;<br>  tags             = &quot;${var.tags}&quot;<br>  vpc_id           = &quot;${var.vpc_id}&quot;<br>}</pre><pre>module &quot;prometheus&quot; {<br>  source           = &quot;../modules/app&quot;<br>  app              = &quot;${var.prometheus}&quot;<br>  env              = &quot;${var.env}&quot;<br>  tags             = &quot;${var.tags}&quot;<br>  vpc_id           = &quot;${var.vpc_id}&quot;<br>  fargate_cpu      = &quot;${var.prometheus[&quot;fargate_cpu&quot;]}&quot;<br>  fargate_memory   = &quot;${var.prometheus[&quot;fargate_memory&quot;]}&quot;<br>  app_count        = &quot;${var.prometheus[&quot;app_count&quot;]}&quot;<br>  app_port         = &quot;${var.prometheus[&quot;app_port&quot;]}&quot;<br>  network_mode     = &quot;${var.prometheus[&quot;network_mode&quot;]}&quot;<br>  hosted_zone      = &quot;${var.prometheus[&quot;hosted_zone&quot;]}&quot;<br>  ecs_cluster      = &quot;${module.ecs.ecs_cluster}&quot;<br>  task-definition-role-arn  = &quot;${module.ecs.rask_definition_role_arn}&quot;<br>  alb-security-group-id     = &quot;${module.security-groups.alb-security-group-id}&quot;<br>  worker-security-group-id  = &quot;${module.security_groups.worker-security-group-id}&quot;<br>}</pre><p>and the variables.tf as follows:</p><pre>variable &quot;env&quot; {<br>  type = map(string)<br>  default = {<br>    environment        = &quot;prometheus-test&quot;<br>    app                = &quot;prometheus&quot;<br>    region             = &quot;<strong>&lt;enter region&gt;</strong>&quot;<br>  }<br>}</pre><pre>variable &quot;tags&quot; {<br>  type = map(string)<br>  default = {<br>    name               = &quot;prometheus-test&quot;<br>    terraform          = &quot;0.12.9&quot;<br>    description        = &quot;Prometheus - build by me&quot; <br>  }<br>}</pre><pre>variable &quot;prometheus&quot; {<br>  type = map(string)<br>  default = {<br>    prom_image     = &quot;<strong>&lt;account&gt;</strong>.dkr.ecr.<strong>&lt;region&gt;</strong>.amazonaws.com/prometheus:latest&quot;<br>    alrt_image     = &quot;<strong>&lt;account&gt;</strong>.dkr.ecr.<strong>&lt;region&gt;</strong>.amazonaws.com/alertmanager:latest&quot;<br>    app_name       = &quot;server&quot;<br>    route53_name   = &quot;prometheus&quot;<br>    app_port       = 9090<br>    fargate_cpu    = 1024<br>    fargate_memory = 2048<br>    app_count      = 1<br>    hosted_zone    = &quot;<strong>&lt;hosted_zone name&gt;</strong>&quot;<br>  }<br>}</pre><pre>variable &quot;vpc_id&quot; { default = &quot;<strong>&lt;VPC&gt;</strong>&quot; }</pre><h3>Final thoughts</h3><p>So the above build isn’t perfect in any way but it works and I’ve used it with some tweaks quite successfully in non-prod and production environments. I’ve been meaning to write this document for some time and a few things have changed in Terraform versions making it easier to achieve the same goals which I plan on implementing in the future.</p><p>The snippets of code that require your own values have been highlighted, with some of the values to be tweaked further to suit your needs. The jmx-exporter entry has been left in the Prometheus.yml file and before you can successfully scrape from the Node Exporter you need to add the prometheus worker security group (port 9100) to the instance you wish to monitor.</p><p>Thank you for taking the time to read this document.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e18e6dbb34f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>