You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询:Rundeck能否自动创建EC2实例节点并执行任务后销毁?

Absolutely! You can absolutely set up Rundeck to spin up EC2 instances, run your jobs on them, and terminate them automatically once the job finishes—even with your specific versions (Rundeck 2.10.2 and rundeck-ec2-nodes-plugin 1.5.5). Here's a practical, step-by-step breakdown to make this work:

Core Concept

The idea is to build a multi-step workflow in Rundeck that:

  1. Provisions a temporary EC2 instance
  2. Adds it as a dynamic Rundeck node
  3. Runs your target job on this node
  4. Cleans up by terminating the instance and removing the node
Step-by-Step Implementation

1. Configure IAM Permissions

First, make sure the Rundeck server has the necessary AWS permissions to manage EC2 instances:

  • If Rundeck runs on an EC2 instance, attach an IAM role with these permissions:
    • ec2:RunInstances (to create instances)
    • ec2:TerminateInstances (to clean up)
    • ec2:DescribeInstances (to fetch instance details like IP)
    • ec2:CreateTags (optional, to label temp instances for easier tracking)
  • If Rundeck runs outside AWS, configure AWS credentials (via ~/.aws/credentials or environment variables) on the Rundeck server.

2. Build the EC2 Provisioning Job

Create a dedicated job to spin up your temp EC2 instance and register it as a Rundeck node:

  1. Create a new job in your Rundeck project.
  2. Add a "Run Script" step with commands to provision the instance (adjust values to match your AWS setup):
    # Launch the EC2 instance
    INSTANCE_ID=$(aws ec2 run-instances \
      --image-id ami-xxxxxx \
      --instance-type t2.micro \
      --security-group-ids sg-xxxxxx \
      --key-name your-key-pair \
      --query 'Instances[0].InstanceId' \
      --output text)
    
    # Tag the instance as a Rundeck temp node
    aws ec2 create-tags --resources $INSTANCE_ID --tags Key=RundeckTemp,Value=Yes
    
    # Wait for the instance to reach "running" state
    aws ec2 wait instance-running --instance-ids $INSTANCE_ID
    
    # Get the instance's public IP
    PUBLIC_IP=$(aws ec2 describe-instances \
      --instance-ids $INSTANCE_ID \
      --query 'Reservations[0].Instances[0].PublicIpAddress' \
      --output text)
    
    # Wait for SSH to become available (critical for Rundeck to connect)
    until nc -z $PUBLIC_IP 22; do sleep 5; done
    
    # Register the instance as a temporary Rundeck node (use Rundeck CLI)
    rd-node add \
      --project YOUR_PROJECT_NAME \
      --name "temp-ec2-$INSTANCE_ID" \
      --hostname $PUBLIC_IP \
      --username ec2-user \
      --ssh-keypath /path/to/your-key-pair.pem
    
    Note: Install the Rundeck CLI (rd command) compatible with 2.10.2 on your Rundeck server if you haven't already.
  3. Export variables: Ensure INSTANCE_ID and PUBLIC_IP are passed to subsequent jobs (use Rundeck's job output variables or project-level variable storage).

3. Build Your Main Workflow Job

Create the main job that ties everything together:

  1. Add a "Run Job" step to execute the EC2 provisioning job, and capture the INSTANCE_ID variable from its output.
  2. Add a "Node Step" to run your target workload on the temporary node (temp-ec2-$INSTANCE_ID). This can be any steps you need—scripts, deployment tasks, etc.
  3. Add a cleanup "Run Script" step to terminate the instance and remove the node (add a failure handler to ensure this runs even if the main job fails):
    # Terminate the EC2 instance
    aws ec2 terminate-instances --instance-ids ${INSTANCE_ID}
    
    # Remove the temporary node from Rundeck
    rd-node remove --project YOUR_PROJECT_NAME --name "temp-ec2-${INSTANCE_ID}"
    

4. Key Notes for Your Rundeck Version

Since you're on Rundeck 2.10.2:

  • Use API version 14 if you prefer interacting with Rundeck via API instead of CLI.
  • The rundeck-ec2-nodes-plugin 1.5.5 is great for discovering existing EC2 instances, but you won't need it for this dynamic provisioning workflow—we're manually adding nodes via CLI/API.
  • Ensure your temp EC2 instance's security group allows inbound SSH from your Rundeck server's IP.

内容的提问来源于stack exchange,提问作者moviss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:55:01