UNHEALTHY Workspaces restart Lambda rewritten to python

Hands Lab Advent Calendar will continue this year as well

Overview

We use Workspaces, a service that provides AWS virtual desktops. Sometimes it falls with Unhealthy

\ It broke even though I didn't do anything /

Most of the time, if the load is temporarily applied and it goes down, restarting will usually help. That's why I have Lambda to restart for the time being Since it is written in Node.js, it supports version upgrades that occur frequently ...

It's a simple code, so I thought I'd rewrite it to python anyway.

Process flow

I wondered why I was checking in order, I wondered if I would not restart by directly specifying the workspace Id that has a problem. Since it will be fetched from the alert information, I interpreted that I did not want to set an alert in CloudWatch for each workspace and did not change it

Result of rewriting

Environmental setting

--Runtime Python 3.7 --Timeout 15 seconds

Source code

import boto3

sns = boto3.client('sns')
workspaces = boto3.client('workspaces')


def lambda_handler(event, context):

  workspaces_client_list = workspaces.describe_workspaces()

  for workspaces_info in workspaces_client_list['Workspaces']:
    workspace_id = workspaces_info['WorkspaceId']
    workspace_state = workspaces_info['State']

    if workspace_state == 'UNHEALTHY':
      reboot_workspaces(workspace_id)


def reboot_workspaces(workspace_id):

  workspaces.reboot_workspaces(
    RebootWorkspaceRequests = [
      {
        'WorkspaceId': workspace_id
      },
    ]
  )

the end

No matter which language you choose, you will not be able to escape from version upgrade support, Recently, the flow of Python has come to our company, so it was nice to have the opportunity to write it.

Tomorrow is @fasahina!

Recommended Posts

UNHEALTHY Workspaces restart Lambda rewritten to python
How to use Python lambda
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Lambda] [Python] Post to Twitter from Lambda!
How to access RDS from Lambda (python)
Connect to s3 with AWS Lambda Python
[Road to intermediate Python] Use lambda expressions
Updated to Python 2.7.9
I was able to recurse in Python: lambda
Sample to send slack notification with python lambda
Export RDS snapshot to S3 with Lambda (Python)
[AWS / Lambda] How to load Python external library
Upload files to Google Drive with Lambda (Python)
Summary of studying Python to use AWS Lambda
"Backport" to python 2
[Introduction to Python] Basic usage of lambda expressions
Write multiple records to DynamoDB with Lambda (Python, JavaScript)
I want to AWS Lambda with Python on Mac!
I was able to repeat it in Python: lambda
Re: Python lambda is useless ^ H ^ H ^ H ^ H ^ H Difficult to use
How to install Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
A python lambda expression ...
Rewrite Python2 code to Python3 (2to3)
How to restart gunicorn
python decorator to retry
Introduction to Python language
Introduction to OpenCV (python)-(2)
Note to daemonize python
Introducing Python 2.7 to CentOS 6.6
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1
Posted as an attachment to Slack on AWS Lambda (Python)
How to import Python library set up in EFS to Lambda
Challenge problem 5 with Python: lambda ... I decided to copy without
[Python] Regularly export from CloudWatch Logs to S3 with Lambda
[Python] Allow pip3 packages to be imported on AWS Lambda
About folder structure when uploading Python module to Lambda Layer