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
질문됨 한 달 전445회 조회
3개 답변
1
수락된 답변

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
답변함 한 달 전
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.

답변함 한 달 전
profile pictureAWS
전문가
검토됨 한 달 전
-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
전문가
답변함 한 달 전
  • this one doesnt spin up new instance

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠