Make it easy to specify the time of AWS CloudWatch Events with CDK.

Since all of them fail, I decided to specify it in JST and convert it to UTC with CDK.

Define CloudWatch Events in CDK

For the time being, only the definition of CloudWatch Events. Whatever you combine this with Step Functions.

#Define batch schedule in JST (cron expression*The place to do is empty)
props['schedule'] = {
    'minute': 40,
    'hour': 9,
    'week_day': 'MON-FRI'
}

#Convert JST to UTC
if props['schedule'].get('hour') is not None:
    props['schedule']['hour'] = props['schedule']['hour'] - 9
    if props['schedule']['hour'] < 0:
        props['schedule']['hour'] += 24
        if props['schedule'].get('day') is not None:
            props['schedule']['day'] = props['schedule']['day'] - 1
        
#JST If it operates by 9 am at the beginning of the month, operate it on a date like the end of the month and judge with Lambda etc.
if props['schedule'].get('day') == 0:
    props['schedule']['day'] = '28-31'

# props['schedule']Convert the contents to str
for key in props['schedule'].keys():
    props['schedule'][key] = str(props['schedule'][key])
    
# Cloud Watch Events
Rule = events.Rule(
    app, f"Batch-{id}",
    schedule=events.Schedule.cron(**props['schedule'])
)

Judgment before 9:00 at the beginning of the month (end of UTC) Lambda

Only whether it is the beginning of the month (1st) of JST is judged and True or False is returned. If you want to incorporate it in StepFunctions, you can judge the return value of this and send it next or finish it, and if it is a simple process, you can continue processing with ʻif result:` in this Lambda.

import datetime
import calendar


def lambda_handler(event, context):
    print('===Start Lambda===')
    print(event)
    
    dt_now = datetime.datetime.now()
    monthrange = calendar.monthrange(dt_now.year, dt_now.month)[1]
    
    result = dt_now.day == monthrange
    
    print(result)
    
    return result

end

I would be grateful if any wise man who knows something better could point out.

Recommended Posts

Make it easy to specify the time of AWS CloudWatch Events with CDK.
When I tried to make a VPC with AWS CDK but couldn't make it
Easy to make with syntax
Make it easy to install the ROS2 development environment with pip install on Python venv
I tried to make a site that makes it easy to see the update information of Azure
To make it possible to connect from the outside (other than localhost) with dev_appserver.py of GAE / Py
[AWS] [GCP] I tried to make cloud services easy to use with Python
[Zaif] I tried to make it easy to trade virtual currencies with Python
I tried to make a url shortening service serverless with AWS CDK
Upload data to s3 of aws with a command and update it, and delete the used data (on the way)
I wanted to use the find module of Ansible2, but it took some time, so make a note
Setting to make the scale and label of the figure easy to see even in the dark theme with google Colaboratory
Use Pillow to make the image transparent and overlay only part of it
A record of the time it took to deploy mysql on Cloud9 + Rails
Specify the start and end positions of files to be included with qiitap
How to specify the NIC to scan with amazon-dash
Specify the Python executable to use with virtualenv
The background of the characters in the text image is overexposed to make it easier to read.
GradCAM with 22 lines of code. tf_explain may be easy to use, I recommend it!
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
The story of trying to contribute to COVID-19 analysis with AWS free tier and failing
It was a little difficult to do flask with the docker version of nginx-unit
I just wanted to extract the data of the desired date and time with Django
It is easy to execute SQL with Python and output the result in Excel
Easy way to check the source of Python modules
Add information to the bottom of the figure with Matplotlib
[Python] How to specify the download location with youtube-dl
Try to get the contents of Word with Golang
Specify MinGW as the compiler to use with Python
[Python] Summary of how to specify the color of the figure
Try to specify the axis with PyTorch's Softmax function
[Selenium] How to specify the relative path of chromedriver?
The sound of tic disorder at work is ... I managed to do it with the code
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
[Introduction to SIR model] Predict the end time of each country with COVID-19 data fitting ♬
How to write offline real time I tried to solve the problem of F02 with Python
Make a note of what you want to do in the future with Raspberry Pi
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
The world's most easy-to-understand explanation of how to make LINE BOT (3) [Linkage with server with Git]
A script that makes it easy to create rich menus with the LINE Messaging API
Return the image data with Flask of Python and draw it to the canvas element of HTML
An easy way to view the time taken in Python and a smarter way to improve it
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
AWS CDK with Python
I tried to find the entropy of the image with python
Specify the project name of docker-compose with Docker integration of Pycharm
I tried to find the average of the sequence with TensorFlow
Make the display of Python module exceptions easier to understand
The first API to make with python Djnago REST framework
You who color the log to make it easier to see
Let's execute the command on time with the bot of discord
Output CloudWatch Logs to S3 with AWS Lambda (Pythyon ver)
Settings to debug the contents of the library with VS Code
Reformat the timeline of the pandas time series plot with matplotlib
Try to automate the operation of network devices with Python
Achieve automatic shutdown of EC2 instances with Lambda + CloudWatch Events
I want to know the legend of the IT technology world
Get the source of the page to load infinitely with python.
Try to extract the features of the sensor data with CNN
Basic calculation of pandas to enjoy Hakone Ekiden while competing with the best members of all time
I tried to unlock the entrance 2 lock sesame with a single push of the AWS IoT button