Skip to content

Instantly share code, notes, and snippets.

@Mosharush
Last active May 19, 2024 23:14
Show Gist options
  • Save Mosharush/cb3a542412ab1ebdcdd37b594689142d to your computer and use it in GitHub Desktop.
Save Mosharush/cb3a542412ab1ebdcdd37b594689142d to your computer and use it in GitHub Desktop.
Import AWS Architecture into Pulumi

Import AWS into Pulumi

Overview

The export-aws-to-pulumi.sh script is a utility for importing AWS resources into Pulumi. It supports importing resources from multiple AWS regions and of multiple types, including EC2 instances, S3 buckets, RDS instances, and VPCs.

Prerequisites

  • AWS CLI installed and configured with appropriate permissions.
  • Pulumi CLI installed.

Usage

  1. Ensure that you have the necessary permissions to access the resources you want to import.

  2. Set the AWS_PROFILE environment variable to the name of the AWS profile you want to use. This ensures that the script runs on the correct AWS account. You can do this by running the following command in your terminal:

export AWS_PROFILE=<your-profile-name>
  1. Run the script:
chmod +x export-aws-to-pulumi.sh 
./export-aws-to-pulumi.sh 

This will generate Pulumi import commands for the AWS resources in the regions specified in the script.

Customization

You can customize the types of resources to import and the regions to import from by modifying the resource_types and regions variables in the script, respectively.

Note

This script generates Pulumi import commands but does not execute them. You will need to manually execute these commands to import the resources into Pulumi.

#!/bin/bash
# Define the AWS resource types you want to import. Add more types as needed.
resource_types=("ec2" "s3" "rds" "vpc")
# Get a list of all AWS regions or define the regions you want to import resources from
# regions=("us-east-1" "us-west-2")
regions=$(aws ec2 describe-regions --query "Regions[*].RegionName" --output text)
# List S3 buckets and generate Pulumi import commands
buckets=$(aws s3api list-buckets --query "Buckets[*].Name" --output text)
for bucket in $buckets; do
echo "pulumi import aws:s3/bucket:Bucket $bucket $bucket"
done
# Loop through each region
for region in $regions; do
# Initialize a flag to check if there are any resources other than VPC
non_vpc_resources_exist=false
# Loop through each resource type
for resource_type in "${resource_types[@]}"; do
case $resource_type in
ec2)
# List EC2 instances
instances=$(aws ec2 describe-instances --region $region --query "Reservations[*].Instances[*].InstanceId" --output text)
if [ -n "$instances" ]; then
non_vpc_resources_exist=true
fi
;;
rds)
# List RDS instances
db_instances=$(aws rds describe-db-instances --region $region --query "DBInstances[*].DBInstanceIdentifier" --output text)
if [ -n "$db_instances" ]; then
non_vpc_resources_exist=true
fi
;;
*)
;;
esac
done
# If there are any resources other than VPC, print the region and resource details
if [ "$non_vpc_resources_exist" = true ]; then
echo "Processing region: $region"
for resource_type in "${resource_types[@]}"; do
echo " Processing resource type: $resource_type"
case $resource_type in
ec2)
# Generate Pulumi import commands for EC2 instances
for instance in $instances; do
echo "pulumi import aws:ec2/instance:Instance $instance $instance"
done
;;
rds)
# Generate Pulumi import commands for RDS instances
for db_instance in $db_instances; do
echo "pulumi import aws:rds/instance:Instance $db_instance $db_instance"
done
;;
vpc)
# List VPCs and generate Pulumi import commands
vpcs=$(aws ec2 describe-vpcs --region $region --query "Vpcs[*].VpcId" --output text)
for vpc in $vpcs; do
echo "pulumi import aws:ec2/vpc:Vpc $vpc $vpc"
done
;;
*)
echo "Unsupported resource type: $resource_type"
;;
esac
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment