forked from aws-samples/aws2tf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path350-get-ecs-cluster.sh
executable file
·122 lines (107 loc) · 4.65 KB
/
350-get-ecs-cluster.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
if [ "$1" != "" ]; then
cmd[0]="$AWS ecs describe-clusters --cluster $1"
pref[0]="clusters"
idfilt[0]="clusterName"
else
cmd[0]="$AWS ecs list-clusters"
pref[0]="clusterArns"
idfilt[0]=""
fi
tft[0]="aws_ecs_cluster"
#rm -f ${tft[0]}.tf
for c in $(seq 0 0); do
cm=${cmd[$c]}
ttft=${tft[(${c})]}
#echo $cm
awsout=$(eval $cm 2>/dev/null)
if [ "$awsout" == "" ]; then
echo "$cm : You don't have access for this resource"
exit
fi
count=$(echo $awsout | jq ".${pref[(${c})]} | length")
if [ "$count" -gt "0" ]; then
count=$(expr $count - 1)
for i in $(seq 0 $count); do
#echo $i
if [ "$1" != "" ]; then
cname=$(echo $awsout | jq ".${pref[(${c})]}[(${i})].${idfilt[(${c})]}" | tr -d '"')
else
cname=$(echo $awsout | jq ".${pref[(${c})]}[(${i})]" | cut -f2 -d'/' | tr -d '"')
fi
echo "$ttft $cname"
fn=$(printf "%s__%s.tf" $ttft $cname)
if [ -f "$fn" ]; then echo "$fn exists already skipping" && continue; fi
printf "resource \"%s\" \"%s\" {" $ttft $cname >$ttft.$cname.tf
printf "}" >>$ttft.$cname.tf
terraform import $ttft.$cname "$cname" | grep Importing
terraform state show -no-color $ttft.$cname >t1.txt
tfa=$(printf "%s.%s" $ttft $cname)
terraform show -json | jq --arg myt "$tfa" '.values.root_module.resources[] | select(.address==$myt)' >data/$tfa.json
#echo $awsj | jq .
rm -f $ttft.$cname.tf
file="t1.txt"
echo $aws2tfmess >$fn
while IFS= read line; do
skip=0
# display $line or do something with $line
t1=$(echo "$line")
if [[ ${t1} == *"="* ]]; then
tt1=$(echo "$line" | cut -f1 -d'=' | tr -d ' ')
tt2=$(echo "$line" | cut -f2- -d'=')
if [[ ${tt1} == "arn" ]]; then skip=1; fi
if [[ ${tt1} == "id" ]]; then skip=1; fi
if [[ ${tt1} == "role_arn" ]]; then skip=1; fi
if [[ ${tt1} == "owner_id" ]]; then skip=1; fi
if [[ ${tt1} == "ipv6_cidr_block_association_id" ]]; then skip=1; fi
#if [[ ${tt1} == "availability_zone" ]];then skip=1;fi
if [[ ${tt1} == "availability_zone_id" ]]; then skip=1; fi
if [[ ${tt1} == "state" ]]; then skip=1; fi
if [[ ${tt1} == "dns_entry" ]]; then skip=1; fi
if [[ ${tt1} == "requester_managed" ]]; then skip=1; fi
if [[ ${tt1} == "prefix_list_id" ]]; then skip=1; fi
if [[ ${tt1} == "capacity_providers" ]]; then
# skip the block
tt2=$(echo $tt2 | tr -d '"')
skip=1
while [ "$t1" != "]" ] && [ "$tt2" != "[]" ]; do
read line
t1=$(echo "$line")
done
fi
if [[ ${tt1} == "cidr_blocks" ]]; then
# skip the block
tt2=$(echo $tt2 | tr -d '"')
skip=1
while [ "$t1" != "]" ] && [ "$tt2" != "[]" ]; do
read line
t1=$(echo "$line")
done
fi
if [[ ${tt1} == "network_interface_ids" ]]; then skip=1; fi
if [[ ${tt1} == "vpc_id" ]]; then
vpcid=$(echo $tt2 | tr -d '"')
t1=$(printf "%s = aws_vpc.%s.id" $tt1 $vpcid)
fi
fi
if [ "$skip" == "0" ]; then
#echo $skip $t1
echo "$t1" >>$fn
fi
done <"$file"
echo "Getting Services $cname"
../../scripts/get-ecs-capacity-providers.sh $cname
../../scripts/get-ecs-service.sh $cname
if [ "$vpcid" != "" ]; then
../../scripts/100-get-vpc.sh $vpcid
fi
done # for i
fi
done #for c
rm -f t*.txt
# $AWS ecs list-attributes --target-type container-instance --cluster myecsprod
# $AWS ecs list-container-instances --cluster myecsprod
# $AWS ecs describe-container-instances --container-instances arn:aws:ecs:eu-west-1:566972129213:container-instance/b05e4516-71c3-487f-a2cf-31b42f7bc722 --cluster myecsprod
# get instance id
# use above to find asg
# $AWS autoscaling describe-auto-scaling-groups