def handler(event, context): """invoke step function handler""" params = get_params() outputs = get_outputs() try: response = client.describe_addresses(Filters=[ { 'Name': 'instance-id', 'Values': [outputs['PlexEc2InstanceId']] }, ]) if (response['Addresses']): event['AssociationId'] = response['Addresses'][0]['AssociationId'] event['AllocationId'] = response['Addresses'][0]['AllocationId'] client.disassociate_address(AssociationId=event['AssociationId']) client.release_address(AllocationId=event['AllocationId']) event['ipReleaseStatus'] = 'IP_RELEASED' else: event['ipReleaseStatus'] = 'IP_ALREADY_RELEASED' except ClientError: event['ipReleaseStatus'] = 'IP_RELEASE_ERROR' return event
def handler(event, context): """invoke step function handler""" outputs = get_outputs() response = client.start_instances(InstanceIds=[ outputs['PlexEc2InstanceId'], ], ) print('event', event) print('response', response) return event
def handler(event, context): """invoke step function handler""" outputs = get_outputs() print('outputs', outputs) response = client.send_command( DocumentName='AWS-RunShellScript', InstanceIds=[ outputs['PlexEc2InstanceId'] ], Comment='set plex claim token and start plex media server', Parameters={ 'commands': [ 'cd /home/ec2-user;' 'docker stop $(docker ps -a -q);', 'docker rm $(docker ps -a -q);', """docker run -d \\ --name plex \\ --restart unless-stopped \\ -p 32400:32400/tcp \\ -p 3005:3005/tcp \\ -p 8324:8324/tcp \\ -p 32469:32469/tcp \\ -p 1900:1900/udp \\ -p 32410:32410/udp \\ -p 32412:32412/udp \\ -p 32413:32413/udp \\ -p 32414:32414/udp \\ -e TZ="America/Los_Angeles" \\ -e PLEX_CLAIM=""" + event["claimToken"] + """\\ -e ADVERTISE_IP=http://0.0.0.0:32400/ \\ -h plex-ec2 \\ -v /home/ec2-user/plex-config/config:/config \\ -v /home/ec2-user/plex-config/transcode:/transcode \\ -v /home/ec2-user/movies:/movies plexinc/pms-docker; """, ], }, CloudWatchOutputConfig={ 'CloudWatchLogGroupName': context.log_group_name, 'CloudWatchOutputEnabled': True, }, TimeoutSeconds=30, ) print('event before', event) print('response', response) event['commandId'] = response['Command']['CommandId'] print('event after', event) return event
def handler(event, context): """invoke step function handler""" outputs = get_outputs() response = client.get_command_invocation( CommandId=event['commandId'], InstanceId=outputs['PlexEc2InstanceId'], ) print('event', event) print('response', response) event['runCommandStatus'] = response['Status'] return event
def handler(event, context): """invoke step function handler""" outputs = get_outputs() try: response = client.describe_instances(InstanceIds=[ outputs['PlexEc2InstanceId'], ], ) instance = response['Reservations'][0]['Instances'][0] event['instanceState'] = instance['State']['Name'] except ClientError: event['instanceStatus'] = "error" return event
def handler(event, context): """invoke step function handler""" outputs = get_outputs() try: response = client.describe_instance_status(InstanceIds=[ outputs['PlexEc2InstanceId'], ], ) print(response) all_statuses = response['InstanceStatuses'][0] instance_status = all_statuses['InstanceState']['Name'] status_checks_ok = all_statuses['InstanceStatus'][ 'Status'] == 'ok' and all_statuses['SystemStatus']['Status'] == 'ok' event['instanceStatus'] = 'STATUS_NOT_OK' if instance_status == 'running' and status_checks_ok: event['instanceStatus'] = 'STATUS_OK' except ClientError: event['instanceStatus'] = "STATUS_ERROR" return event
def handler(event, context): """invoke step function handler""" params = get_params() outputs = get_outputs() print('plex-ip', params['plex-ip']) print('outputs', outputs['PlexEc2InstanceId']) try: response = client.describe_addresses(Filters=[ { 'Name': 'instance-id', 'Values': [outputs['PlexEc2InstanceId']] }, ]) if (response['Addresses']): event['ipAttachStatus'] = 'IP_ALREADY_ATTACHED' else: allocate_response = client.allocate_address() print('allocate_response', allocate_response) associate_response = client.associate_address( AllocationId=allocate_response['AllocationId'], InstanceId=outputs['PlexEc2InstanceId']) print('associate_response', associate_response) ssm_client.put_parameter( Name='/plex-ec2/plex-ip', Value=allocate_response['PublicIp'], Type='String', Overwrite=True, ) event['AssociationId'] = associate_response['AssociationId'] event['AllocationId'] = allocate_response['AllocationId'] event['PlexIp'] = allocate_response['PublicIp'] event['ipAttachStatus'] = 'IP_ATTACHED' except ClientError: event['ipAttachStatus'] = 'IP_ATTACH_ERROR' return event