Last Updated:

Tidy Cloud AWS issue 38 - Holiday greetings from ChatGPT

Erik Lundevall-Zara
Erik Lundevall-Zara Bulletin

Hello all!

Welcome to the next issue of the Tidy Cloud AWS bulletin! In this issue, we are taking a bit of holiday break and I have asked ChatGPT to help out a bit with this issue. I wish you all a Merry Christmas and a Happy New Year!

Enjoy!


ChatGPT introduction

If you have not heard of ChatGPT before, I asked it to explain itself to you. You can try it out yourself at https://chat.openai.com. My written questions are in bold italics, the ChatGPT answers in italics below.

Explain what ChatGPT is and its value, in simple terms?

ChatGPT is a machine learning model developed by OpenAI that is designed to generate human-like text in response to prompts. It is based on the GPT (Generative Pre-training Transformer) architecture, which is a type of model that has achieved state-of-the-art performance on a variety of natural language processing tasks.

The value of ChatGPT lies in its ability to generate coherent and expressive text that can be used in a variety of applications, such as chatbots, content generation, language translation, and dialogue systems. By providing a natural and intuitive way to communicate with machines, ChatGPT can help to improve the user experience and functionality of a wide range of applications that involve natural language processing.

For example, ChatGPT could be used to create a chatbot that can hold a conversation with a user in a natural and engaging way, or to generate content for a website or social media platform. It could also be used to translate text from one language to another, or to generate responses to customer inquiries in a customer service application.

Overall, the value of ChatGPT lies in its ability to generate human-like text that can be used to improve the usability and functionality of a wide range of applications that involve natural language processing.

Answer to a technical question

ChatGPT is able to provide answers to technical questions, but far from all answers are good answers, in particular if it relates to less common topics. This is not surprising, since the value of the answers depending on the amount of training data. In some cases it can be quicker than to look up things in the documentation though.

Can you show an example of an AWS lambda function that deletes a CloudFormation stack 3 days after it has been created, using Python?

Certainly! Here is an example of an AWS Lambda function written in Python that will delete a CloudFormation stack three days after it has been created:

import boto3
import datetime

def delete_stack(stack_name):
    # Get the CloudFormation client
    cloudformation = boto3.client('cloudformation')

    # Delete the stack
    cloudformation.delete_stack(StackName=stack_name)

def lambda_handler(event, context):
    # Get the stack name from the event
    stack_name = event['StackName']

    # Get the current time
    now = datetime.datetime.now()

    # Get the stack creation time
    stack_creation_time = datetime.datetime.strptime(event['StackCreationTime'], '%Y-%m-%dT%H:%M:%S.%fZ')

    # Calculate the difference between the current time and the stack creation time
    time_difference = now - stack_creation_time

    # If the stack has been created for more than three days, delete it
    if time_difference.days > 3:
        delete_stack(stack_name)

This function expects to receive an event object with two keys: StackName and StackCreationTime. The StackName is the name of the stack that you want to delete, and the StackCreationTime is the time that the stack was created, in the format '%Y-%m-%dT%H:%M:%S.%fZ'.

To use this function, you will need to create an AWS Lambda function and set the trigger to run on a schedule. For example, you can set the function to run every hour by using a CloudWatch Events rule with a cron expression like 0 * * * ? *.

I hope this helps! Let me know if you have any questions.

Can you modify the lambda code to use a date to delete which set in a tag on the CloudFormation stack?

Certainly! Here is a modified version of the AWS Lambda function that will delete a CloudFormation stack based on the value of a tag on the stack:

import boto3
import datetime

def delete_stack(stack_name):
    # Get the CloudFormation client
    cloudformation = boto3.client('cloudformation')

    # Delete the stack
    cloudformation.delete_stack(StackName=stack_name)

def get_stack_delete_date(stack_name):
    # Get the CloudFormation client
    cloudformation = boto3.client('cloudformation')

    # Get the stack
    stack = cloudformation.describe_stacks(StackName=stack_name)

    # Get the tags for the stack
    tags = stack['Stacks'][0]['Tags']

    # Find the tag with the key 'DeleteOn'
    delete_on_tag = next((tag for tag in tags if tag['Key'] == 'DeleteOn'), None)

    # If the tag was found, return the value as a datetime object
    if delete_on_tag:
        return datetime.datetime.strptime(delete_on_tag['Value'], '%Y-%m-%d')

    # If the tag was not found, return None
    return None

def lambda_handler(event, context):
    # Get the stack name from the event
    stack_name = event['StackName']

    # Get the current time
    now = datetime.datetime.now()

    # Get the stack delete date from the tag
    delete_date = get_stack_delete_date(stack_name)

    # If the stack delete date is set and is in the past, delete the stack
    if delete_date and delete_date <= now:
        delete_stack(stack_name)

This function expects to receive an event object with a single key: StackName, which is the name of the stack that you want to delete. It will then look for a tag on the stack with the key DeleteOn, and if it is found, it will parse the value of the tag as a date in the format '%Y-%m-%d'. If the delete date is in the past, the function will delete the stack.

To use this function, you will need to create an AWS Lambda function and set the trigger to run on a schedule. For example, you can set the function to run every hour by using a CloudWatch Events rule with a cron expression like 0 * * * ? *.

I hope this helps! Let me know if you have any questions.

Christmas greetings

Many of you may already have celebrated Chistmas by the time you read this. Anyway, I got a bit of help to get a Christmas song with a bit of infrastructure as code flavour:

Write new lyrics for White Christmas which includes infrastructure as code, Pulumi, AWS CDK and Terraform

*Verse 1: I’m dreaming of a white Christmas, just like the ones I used to know Where the infrastructure’s deployed, with code that’s neat and clean and orderly I’m dreaming of a white Christmas, with every IaC tool I use Pulumi, AWS CDK, and Terraform too

*Chorus: May your Christmas be white, and your deployments be bright May your infrastructure be strong, and your code be right May your Christmas be white, with Pulumi, CDK, and Terraform May all your deployments go smoothly, and your infrastructure transform

Verse 2: I’m dreaming of a white Christmas, with every deployment a success No downtime or errors, no stress or duress I’m dreaming of a white Christmas, where my infrastructure runs smooth and fine With Pulumi, CDK, and Terraform in line

Chorus: May your Christmas be white, and your deployments be bright May your infrastructure be strong, and your code be right May your Christmas be white, with Pulumi, CDK, and Terraform May all your deployments go smoothly, and your infrastructure transform

Outro: I’m dreaming of a white Christmas, with every deployment in place May your Christmas be white, with Pulumi, CDK, and Terraform in the race May all your deployments go smoothly, and your infrastructure transform I’m dreaming of a white Christmas, with every IaC tool in form


You can find older bulletins and more at Tidy Cloud AWS. You will also find other useful articles around AWS automation and infrastructure-as-software.

Until next time,

/Erik