🚀 Step 1: Set Up Users and Permissions in AWS
First, you need to create a dedicated IAM user and grant it the necessary permissions to interact with AWS services.
Create an IAM User: (for setting up an AWS account in n8n)
- Access the AWS Management Console, go to the IAM service, then select Users from the left-hand menu and click Create user.
- Give the user a name (e.g.,
n8n-ec2-controller) and click Next. - Attach policies:
- Select Attach policies directly.
- In the search box, type and select the following policies:
AWSLambda_FullAccess: Grants n8n permission to call Lambda functions.AmazonEC2FullAccess: Grants Lambda permission to start/stop EC2 servers.
- Click Next, then Create user.
Create an Access Key:
- Go back to the Users section and select the user you just created.
- Open the Security credentials tab and click Create access key.
- Choose Third-party service or Application running on my own premises, then click Next.
- Click Create access key and copy the Access key ID and Secret access key that are displayed. Note: Save the Secret key immediately as you will not be able to view it again.(Use this to set up the AWS account in n8n)





Create an IAM Role: (to link with Lambda)
- In the IAM interface, click the Create role button in the top right corner.
- Select a Service: In the Select trusted entity section, choose AWS service.
- In the Use case section, select Lambda, then click Next. This ensures the role is configured for use with AWS Lambda.
- Find and Attach Policies: This is the most important step. In the “Add permissions” search bar, type
AWSLambdaBasicExecutionRole. Select this policy from the search results. Continue searching for and selectAmazonEC2FullAccess(or a lower-privilege policy likeAmazonEC2ReadOnlyAccessif you only want to read information). - After selecting both policies, click Next.
- Name and Create the Role: Give your Role a name (e.g.,
Lambda_EC2_Control_Role). You can add a description if needed. - Review the attached policies and click Create role to finish.
Once this role is created, you can assign it to your Lambda function so the function has the necessary permissions to access the EC2 service.

🛠️ Step 2: Write and Deploy Lambda Functions
You’ll write two Lambda functions using Python to handle the start and stop commands.
Create the start-ec2-instance Function:
- In AWS, go to Lambda, select Create function.
- Choose Author from scratch, name it
start-ec2-instance, select Python 3.12 as the Runtime, and choose the IAM Role you created in the previous step. - Paste the following code into the function editor: This code has been optimized to handle both a string or a list of instance IDs.
Python
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name='us-east-2')
try:
# Get the value from the payload with the 'instance_ids' key
instance_ids = event['instance_ids']
# If the value is a string, wrap it in a list
if isinstance(instance_ids, str):
instance_ids = [instance_ids]
# Start the EC2 instances
ec2.start_instances(InstanceIds=instance_ids)
print(f'Starting EC2 instances: {instance_ids}')
return {
'statusCode': 200,
'body': f'Successfully started instances {instance_ids}'
}
except Exception as e:
print(f'Error starting instances: {e}')
return {
'statusCode': 500,
'body': f'Error starting instances: {e}'
}
- In Configuration > General configuration, increase the Timeout to 10 seconds.
- Click Deploy to save and deploy the function.
Create the stop-ec2-instance Function:
- Repeat the steps above, name it
stop-ec2-instanceand replaceec2.start_instanceswithec2.stop_instancesin the code.
Python
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name='us-east-2')
try:
# Get the value from the payload with the 'instance_ids' key
instance_ids = event['instance_ids']
# If the value is a string, wrap it in a list
if isinstance(instance_ids, str):
instance_ids = [instance_ids]
# Stop the EC2 instances
ec2.stop_instances(InstanceIds=instance_ids)
print(f'Stopping EC2 instances: {instance_ids}')
return {
'statusCode': 200,
'body': f'Successfully stopped instances {instance_ids}'
}
except Exception as e:
print(f'Error stopping instances: {e}')
return {
'statusCode': 500,
'body': f'Error stopping instances: {e}'
}
- In Configuration > General configuration, increase the Timeout to 10 seconds.
- Click Deploy to save and deploy the function.
⚙️ Step 3: Configure the Workflow in n8n
This step will connect the components you’ve created to automate the process.
Set up AWS Credential in n8n:
- In n8n, go to Credentials, select Create New, and find AWS Credential.
- Paste the Access key ID and Secret access key you obtained in Step 1.
Create a Workflow:
- Add a Schedule Trigger node to set up a schedule. For example, to run every day at 7:30 PM, set the Trigger Interval to
Custom (Cron)and the Expression to0 30 19 * * ?. - Add an AWS Lambda node and connect it to the Schedule Trigger node.
Configure the AWS Lambda Node:
- Credential: Select the AWS credential you created.
- Function Name: Enter the name of the Lambda function (e.g.,
stop-ec2-instance). - Qualifier: Enter
$LATESTto use the latest function version. - Invocation Type: Select
RequestResponse. - JSON Input: This is where you pass the server ID.Scenario 1 (one ID):JSON
{ "instance_ids": "i-0a5c09e1829af388b" }Scenario 2 (multiple IDs):JSON{ "instance_ids": ["i-0a5c09e1829af388b", "i-0180b664bdcd04d8e"] }
After you’re done, activate the workflow, and it will automatically run according to the schedule you’ve set.