<?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 K1LADUB on Medium]]></title>
        <description><![CDATA[Stories by K1LADUB on Medium]]></description>
        <link>https://medium.com/@d_dublya21?source=rss-239508d8844f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*xjBEopTjsmiWcWus</url>
            <title>Stories by K1LADUB on Medium</title>
            <link>https://medium.com/@d_dublya21?source=rss-239508d8844f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:03:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@d_dublya21/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[AWS Lambda trigger for logging a JIRA ticket on a 3 week rotation]]></title>
            <link>https://medium.com/@d_dublya21/aws-lambda-trigger-for-logging-a-jira-ticket-on-a-3-week-rotation-ef2deb4d176b?source=rss-239508d8844f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef2deb4d176b</guid>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[jira]]></category>
            <category><![CDATA[3-week-rotation]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[K1LADUB]]></dc:creator>
            <pubDate>Fri, 19 Oct 2018 12:52:23 GMT</pubDate>
            <atom:updated>2018-10-19T12:52:23.973Z</atom:updated>
            <content:encoded><![CDATA[<p>Jeez, now that’s a super specific topic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/191/1*a1w-tzbE5yaX4HoXJ68lJA.png" /></figure><p>If you don’t want to read the crap then the code is at the bottom but I do use some specifics here that might need a bit of background. I assume you already know how to use Lambda, parameter store, S3, and know your JIRA API endpoint. Remember parameter store data should be stored as encrypted using KMS. If you have another method of storing secure details then use them instead of the parameter store stuff I have here.</p><p>I had a requirement to remind engineers to run DB backup checks on 3 production databases, prod1, prod2, and prod3 (not their real names of course), and on the 4th week they rested, on a Thursday.</p><p>We use AWS and we use JIRA. So the solution would be a triggered event each week in Lambda to log a ticket in JIRA.</p><p>Sounds easy? Well it’s not that simple due to the week rotation. It would have been nice if it was week 1 of the month = prod1, week 2 of the month = prod2 etc, then a cron job could have done that for me, but it was a system that required the 3 DBs and one week off in order only — regardless of week. Eventually what was on week1 in one month would end up as week 4 in a later month and we didn’t want to skip a notification on the year turn over.</p><p>The elements below use parameter store to store our JIRA username and password. I will not cover setting up your Lambda role access to both SSM and S3, suffice to say you will need a role in which to run your Lambda script that has permissions to the AWS resources you intend to use, in this case parameter store (SSM) and S3.</p><p>I used a CloudWatch cron job as a trigger for every Thursday and let the code determine what the script should do, or if it should run at all. The cron job is:</p><pre>0 9 ? * THU * </pre><p>Most of the code is a pretty straight forward request to JIRA containing a JSON payload but where the head scratcher came in was the weekly rotation. As stated above we ruled out an AWS cron job and as this function is serverless we couldn’t store any kind of state. Locally that is. We could store state on S3.</p><p>So I devised the rotation plan, prod1, prod2, prod3, none, and back around again. Wrote some if, elif statements in Python that determined the current week’s DB type, used the name, appended the date, renamed the file on S3 to next week’s DB type and deleted the current file. Easy thinking looking back on it, but it took a while.</p><p>So I created an S3 bucket and and ran the whole lot for test and it all worked good. Not the first time tho, or even the second. Quite a few iterations I would say.</p><p>Some things to look out for:</p><p>There should only ever be one file in that bucket or else you will have continuity issues.</p><p>Lambda sometimes triggers jobs twice — Ive seen this in other scripts with CloudWatch based cron job triggers — this will not work good here — I will keep an eye on this.</p><p>Also dont forget after your import lines to add your:</p><pre>def lambda_handler(event, context):</pre><p>And indent accordingly on the way down.</p><p>Enjoy and any issues let me know.</p><p>Code:</p><pre>#!/usr/local/bin/python<br>import sys<br>import json<br>import urllib2<br>import base64<br>import boto3<br>from boto3 import client<br>from datetime import date</pre><pre># get login details from Parameter Store<br>ssm = boto3.client(‘ssm’,region_name=”us-east-1&quot;)<br>ssm_user = ssm.get_parameter(Name=”JIRAuser”,WithDecryption=True)[‘Parameter’][‘Value’]<br>ssm_password = ssm.get_parameter(Name=”JIRApassword”,WithDecryption=True)[‘Parameter’][‘Value’]</pre><pre># set vars<br>username = ssm_user<br>password = ssm_password<br>today = date.today()<br>s3BucketName = “db-check”<br>s3 = client(‘s3’)</pre><pre># get current DB job<br>for key in s3.list_objects(Bucket=s3BucketName)[‘Contents’]:<br> currentDB = (key[‘Key’])</pre><pre># create function to copy file to next week’s DB type then delete the current file<br>def bucketOperation(newFile, oldFile):<br> newPath = “%s/%s” % (s3BucketName, oldFile)<br> s3Object = boto3.resource(‘s3’)<br> s3Object.Object(s3BucketName, newFile).copy_from(CopySource=newPath)<br> s3Object.Object(s3BucketName,oldFile).delete()</pre><pre># logic for DB type this week<br>if currentDB == “prod1.txt”:<br> dbTag = “Prod1 Restore Test %s/%s/%s” % (today.day, today.month, today.year)<br> nextDB = “prod2.txt”<br> bucketOperation(nextDB, currentDB)<br>elif currentDB == “prod2.txt”:<br> dbTag = “Prod2 Restore Test %s/%s/%s” % (today.day, today.month, today.year)<br> nextDB = “prod3.txt”<br> bucketOperation(nextDB, currentDB)<br>elif currentDB == “prod3.txt”:<br> dbTag = “Prod3 Restore Test %s/%s/%s” % (today.day, today.month, today.year)<br> nextDB = “none.txt”<br> bucketOperation(nextDB, currentDB)<br>elif currentDB == “none.txt”:<br> nextDB = “prod1.txt”<br> bucketOperation(nextDB, currentDB)<br> sys.exit()</pre><pre># JIRA functions<br># set endpoint<br>JiraEndPoint = ‘<a href="https://your.jira.url.net/rest/api/2/issue/&#39;">https://your.JIRA.URL.net/rest/api/2/issue/&#39;</a></pre><pre># encode JIRA login details<br>myAuthString = base64.standard_b64encode(‘%s:%s’ % (username, password))</pre><pre># set values for ticket<br>json_values = {<br> “fields”: {<br> “labels”: [“BackupDBTest”],<br> “project”: {“key”: “DEVOPS”},<br> “summary”: dbTag,<br> “description”: “Please follow the steps outlined in: “<br> “&lt;URL to confluence page here&gt;”<br> “This will test if our backups functions are working correctly”,<br> “issuetype”: {“name”: “Task”}<br> }<br>}</pre><pre># create the payload for the URL request<br>response = urllib2.Request(JiraEndPoint, data=json.dumps(json_values), headers={‘Authorization’: “Basic %s” % myAuthString, ‘Content-Type’: ‘application/json’})<br># make the URL request<br>resp = urllib2.urlopen(response)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef2deb4d176b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>