usage() { echo "Usage: $0 [-p (Default="default") ] [-g ] [-t ] [-r ] [-x ]" 1>&2; exit 1; } x="no" p="default" f="no" v="no" r="no" t="*" #while getopts ":p:g:r:x:t:" o; do while getopts ":p:r:x:f:v:t:" o; do case "${o}" in # a) # s=${OPTARG} # ;; t) t=${OPTARG} ;; r) r=${OPTARG} ;; x) x="yes" ;; p) p=${OPTARG} ;; f) f="yes" ;; v) v="yes" ;; *) usage ;; esac done shift $((OPTIND-1)) export aws2tfmess="# File generated by aws2tf see https://github.com/andyt530/aws2tf" mysub=`aws sts get-caller-identity --profile $p | jq .Account | tr -d '"'` if [ "$r" = "no" ]; then echo "Region not specified - Getting region from aws cli =" r=`aws configure get region` echo $r fi if [ "$mysub" == "null" ]; then echo "Account is null exiting" exit fi mkdir -p generated/tf.$mysub s=`echo $mysub` cd generated/tf.$mysub rm -rf .terraform if [ "$f" = "no" ]; then rm -f import.log resources*.txt rm -f processed.txt rm -f *.tf rm -f terraform.* rm -rf .terraform else sort -u processed.txt > pt.txt cp pt.txt processed.txt fi #if [ "$f" = "no" ]; then # ../../scripts/resources.sh 2>&1 | tee -a import.log #fi export AWS="aws --profile $p --region $r" echo " " echo "Account ID = ${s}" echo "AWS Resource Group Filter = ${g}" echo "Region = ${r}" echo "AWS Profile = ${p}" echo "Extract KMS Secrets to .tf files (insecure) = ${x}" echo "Fast Forward = ${f}" echo "Verify only = ${v}" echo "AWS command = ${AWS}" echo " " # cleanup from any previous runs rm -f terraform*.backup #rm -f terraform.tfstate rm -f tf*.sh cp ../../stub/*.tf . # write the aws.tf file printf "provider \"aws\" {\n" > aws.tf printf " region = \"%s\" \n" $r >> aws.tf printf " shared_credentials_file = \"~/.aws/credentials\" \n" >> aws.tf printf " version = \">= 2.53\" \n" >> aws.tf printf " profile = \"%s\" \n" $p >> aws.tf printf "}\n" >> aws.tf export AWS="aws --profile $p --region $r" cat aws.tf pwd echo "terraform init" terraform init 2>&1 | tee -a import.log ###################################################### # list of cluster names echo "clusters" clusters=() for i in `aws ecs list-clusters | jq .clusterArns[]`; do cl=`echo $i | tr -d '"' | cut -f2 -d'/'` clusters+=("$cl") done len=${#clusters[@]} echo $len for (( i=0; i<$len; i++ )); do echo "${clusters[$i]}" done vpcs=() cinst=`aws ecs list-container-instances --cluster myecsprod | jq .containerInstanceArns[0] | tr -d '"' | cut -f2 -d'/'` echo "cinst=$cinst" if [ $cinst != "null" ]; then inst=`aws ecs describe-container-instances --cluster myecsprod --container-instances $cinst | jq .containerInstances[0].ec2InstanceId | tr -d '"'` echo $inst vpc=`aws ecs describe-container-instances --cluster myecsprod --container-instances $inst | jq '.containerInstances[0].attributes[] | select(.name=="ecs.vpc-id").value'| tr -d '"'` echo $vpc vpcs+=("$vpc") fi aserv=`aws ecs list-services --cluster myecsprod | jq .serviceArns[0] | tr -d '"'` echo $aserv asubnet=`aws ecs describe-services --cluster myecsprod --services $aserv | jq .services[0].networkConfiguration.awsvpcConfiguration.subnets[0] | tr -d '"'` echo $asubnet comm=`printf "aws ec2 describe-subnets | jq '.Subnets[] | select(.SubnetId==\"%s\").VpcId'" $asubnet` echo $comm exit echo "terraform init" terraform init 2>&1 | tee -a import.log len=${#vpcs[@]} echo $len for (( i=0; i<$len; i++ )); do echo "${vpcs[$i]}" ../../vpc2tf.sh ${vpcs[$i]} ../../asg2tf.sh ${vpcs[$i]} ../../elbv2.sh ${vpcs[$i]} done # get vpc # get asg # get alb ../../scripts/351-get-ecs-task.sh len=${#clusters[@]} echo $len for (( i=0; i<$len; i++ )); do echo "${clusters[$i]}" ../../scripts/350-get-ecs-cluster.sh ${clusters[$i]} done # gather ECS resources - from container instances FilesToExtarct=() for file in $@; do FilesToExtarct+="$file.csv " done # NF - predefined awk number of fields - so just the last field - no timestamp,size fileList=$($AWS s3 ls s3://$inputbucket/ | awk '{print $NF}') for file in ${fileList[@]}; do echo "${#distro[@]}" for (( i=0; i<$len; i++ )); do echo "${distro[$i]}" ; done # gather ECS resources - from services date pwd lc=0 echo "t=$t" echo "loop through providers" pwd for com in `ls ../../scripts/*-get-$t.sh | cut -d'/' -f4 | sort -g`; do echo "$com" docomm="../../scripts/$com" if [ "$f" = "no" ]; then eval $docomm 2>&1 | tee -a import.log else grep "$docomm" processed.txt if [ $? -eq 0 ]; then echo "skipping $docomm" else eval $docomm 2>&1 | tee -a import.log fi fi lc=`expr $lc + 1` file="import.log" while IFS= read -r line do if [[ "${line}" == *"Error"* ]];then if [[ "${line}" == *"Duplicate"* ]];then echo "Ignoring $line" else echo "Found Error: $line exiting .... (pass for now)" pass fi fi done <"$file" echo "$docomm" >> processed.txt rm -f terraform*.backup done date #if [ "$x" = "yes" ]; then # echo "Attempting to extract secrets" # ../../scripts/kms_secrets.sh #fi echo "---------------------------------------------------------------------------" echo "aws2tf output files are in generated/tf.$mysub" echo "---------------------------------------------------------------------------" echo "terraform fmt > /dev/null ..." terraform fmt > /dev/null echo "Terraform validate ..." terraform validate . if [ "$v" = "yes" ]; then exit fi echo "Terraform Plan ..." terraform plan .