How to run EC2 instance with Step Function

0

How can I run an instance using Step function? The best if it can by run sync way with outputting instance ID :)

Kibel
asked 16 days ago152 views
3 Answers
1
Accepted Answer

You can use Step Functions AWS SDK Service Integrations to call the EC2 API directly. Below is Amazon States Language for a simple example (you would need to update the specified security group and you need to ensure the Execution Role for your state machine has the necessary privs to make the call to the EC2 API).

An easy way to build this stuff up is to first go to the EC2 Console where you can build up the instance type you want, then use the Review Commands link to show the API parameters: Enter image description here

Then you can just paste those into the API Parameters for your Task State in Step Functions: Enter image description here

And if you want to do multiple steps, such as create a new security group or look up such info, you can do that in previous workflow steps and pass into your RunInstances call.

{
  "StartAt": "RunInstances",
  "States": {
    "RunInstances": {
      "Type": "Task",
      "End": true,
      "Parameters": {
        "MaxCount": 1,
        "MinCount": 1,
        "ImageId": "ami-0663b059c6536cac8",
        "InstanceType": "t2.micro",
        "EbsOptimized": false,
        "NetworkInterfaces": [
          {
            "AssociatePublicIpAddress": true,
            "DeviceIndex": 0,
            "Groups": [
              "sg-f8717ed8"
            ]
          }
        ],
        "MetadataOptions": {
          "HttpEndpoint": "enabled",
          "HttpPutResponseHopLimit": 2,
          "HttpTokens": "required"
        },
        "PrivateDnsNameOptions": {
          "HostnameType": "ip-name",
          "EnableResourceNameDnsARecord": true,
          "EnableResourceNameDnsAAAARecord": false
        }
      },
      "Resource": "arn:aws:states:::aws-sdk:ec2:runInstances"
    }
  }
}
AWS
answered 16 days ago
0

Hi , There is multiple ways to we can run EC2 with step function.

  1. invoke lambda from step function in lambda, need to implement run the EC2 instance
  2. invoke SSM from step function

Both ways we can run EC2 instance from step function

Try below code snippet to create instance in boto3

import json import boto3 import os import time

ec2_client = boto3.client("ec2", region_name=os.environ['AWS_REGION'])

def create_instance(): instances = ec2_client.run_instances( ImageId=os.environ['ami'], MinCount=1, MaxCount=1, SecurityGroupIds=[os.environ['security_group']], InstanceType=os.environ['instance_type'], SubnetId=os.environ['subnet_id'], IamInstanceProfile={'Name': 'ec2-instance-role'} ) instance_id = instances["Instances"][0]["InstanceId"]

max_time = 800 # 10min
start_time = 0
health_check = False

while start_time <= max_time:
    response = ec2_client.describe_instance_status(InstanceIds=[instance_id])
    for instance in response['InstanceStatuses']:
        print("EC2 System status:%s" %instance['SystemStatus']['Status'])
        if instance['SystemStatus']['Status'] == 'initializing':
            continue
        elif instance['SystemStatus']['Status'] == 'ok':
            health_check = True

            break
    print("Health Check:%s" %health_check)
    if health_check:
        break
    else:
        # wait for a min for next iteration
        time.sleep(60)
        start_time += 60 
        continue

if not health_check:
    return {
        'statusCode': 400,
        'body': "The Instance %s health check failed" %instances["Instances"][0]["InstanceId"]
    }

return instances["Instances"][0]["InstanceId"]

def lambda_handler(event, context): try: instanceId = create_instance()
return { 'statusCode': 200, 'body': json.dumps({'instance_id': instanceId}) } except Exception as e: return { 'statusCode': 400, 'body': 'Creation of EC2 instance failed:%s' %e }

By invoking above function before, need to add subnet, security role etc. which are defined in OS.

answered 16 days ago
profile pictureAWS
EXPERT
reviewed 16 days ago
-1

Hi,

You have a full example in this article: https://www.pulumi.com/ai/answers/c9F7weofui2LaFB1if821v/orchestrating-aws-ec2-instances-with-sfn

Choose your favorite language at bottom of article.

Didier

profile pictureAWS
EXPERT
answered 16 days ago
  • this one doesnt spin up new instance

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions