@@ -23,66 +23,69 @@ resource "null_resource" "deploy" {
23
23
24
24
provisioner "remote-exec" {
25
25
inline = [
26
+ <<- EOF
26
27
# Make the Terraform fail if any step throws an error
27
- " set -e" ,
28
+ set -e
28
29
29
30
# Ensure environment is clean
30
- " echo \ " LOG: Rerunning cleanup commands in case of cleanup failure in previous run\" " ,
31
- " helm uninstall --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator --ignore-not-found" ,
32
- " kubectl delete namespace sample-app-namespace --ignore-not-found=true" ,
33
- " [ ! -e amazon-cloudwatch-agent-operator ] || sudo rm -r amazon-cloudwatch-agent-operator" ,
34
- " [ ! -e frontend-service-depl.yaml ] || rm frontend-service-depl.yaml" ,
35
- " [ ! -e remote-service-depl.yaml ] || rm remote-service-depl.yaml" ,
31
+ echo "LOG: Rerunning cleanup commands in case of cleanup failure in previous run"
32
+ helm uninstall --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator --ignore-not-found
33
+ kubectl delete namespace sample-app-namespace --ignore-not-found=true
34
+ [ ! -e amazon-cloudwatch-agent-operator ] || sudo rm -r amazon-cloudwatch-agent-operator
35
+ [ ! -e frontend-service-depl.yaml ] || rm frontend-service-depl.yaml
36
+ [ ! -e remote-service-depl.yaml ] || rm remote-service-depl.yaml
36
37
37
38
# Clone and install operator onto cluster
38
- " echo \ " LOG: Cloning operator repo\" " ,
39
- " git clone https://github.com/aws/amazon-cloudwatch-agent-operator -q" ,
40
- " cd amazon-cloudwatch-agent-operator/helm/" ,
41
- " echo \ " LOG: Installing CloudWatch Agent Operator using Helm\" " ,
42
- " helm upgrade --install --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator ./ --create-namespace --set region=us-east-1 --set clusterName=k8s-cluster-${ var . test_id } " ,
39
+ echo "LOG: Cloning operator repo"
40
+ git clone https://github.com/aws/amazon-cloudwatch-agent-operator -q
41
+ cd amazon-cloudwatch-agent-operator/helm/
42
+ echo "LOG: Installing CloudWatch Agent Operator using Helm"
43
+ helm upgrade --install --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator ./ --create-namespace --set region=us-east-1 --set clusterName=k8s-cluster-${ var . test_id }
43
44
44
- " sleep 60" , # wait for pods to exist before checking if they're ready
45
- " kubectl wait --for=condition=Ready pods --all --selector=app.kubernetes.io/name=amazon-cloudwatch-observability -n amazon-cloudwatch --timeout=60s" ,
46
- " kubectl wait --for=condition=Ready pods --all --selector=app.kubernetes.io/name=cloudwatch-agent -n amazon-cloudwatch --timeout=60s" ,
45
+ sleep 60 # wait for pods to exist before checking if they're ready
46
+ kubectl wait --for=condition=Ready pods --all --selector=app.kubernetes.io/name=amazon-cloudwatch-observability -n amazon-cloudwatch --timeout=60s
47
+ kubectl wait --for=condition=Ready pods --all --selector=app.kubernetes.io/name=cloudwatch-agent -n amazon-cloudwatch --timeout=60s
47
48
48
49
# Create sample app namespace
49
- " echo \ " LOG: Creating sample app namespace\" " ,
50
- " kubectl create namespace sample-app-namespace" ,
50
+ echo "LOG: Creating sample app namespace"
51
+ kubectl create namespace sample-app-namespace
51
52
52
53
# Set up secret to pull image with
53
- " echo \ " LOG: Creating secret \" " ,
54
- " ACCOUNT=$(aws sts get-caller-identity --query 'Account' --output text)" ,
55
- " SECRET_NAME=ecr-secret" ,
56
- " TOKEN=`aws ecr --region=${ var . aws_region } get-authorization-token --output text --query authorizationData[].authorizationToken | base64 -d | cut -d: -f2`" ,
54
+ echo "LOG: Creating secret "
55
+ ACCOUNT=$(aws sts get-caller-identity --query 'Account' --output text)
56
+ SECRET_NAME=ecr-secret
57
+ TOKEN=`aws ecr --region=${ var . aws_region } get-authorization-token --output text --query authorizationData[].authorizationToken | base64 -d | cut -d: -f2`
57
58
58
- " echo \ " LOG: Deleting secret if it exists\" " ,
59
- " kubectl delete secret -n sample-app-namespace --ignore-not-found $SECRET_NAME" ,
60
- " echo \ " LOG: Creating secret for pulling sample app ECR\" " ,
61
- " kubectl create secret -n sample-app-namespace docker-registry $SECRET_NAME \\ " ,
62
- " --docker-server=https://$ACCOUNT.dkr.ecr.${ var . aws_region } .amazonaws.com \\ " ,
63
- " --docker-username=AWS \\ " ,
64
- " --docker-password=\ " $${TOKEN}\" " ,
59
+ echo "LOG: Deleting secret if it exists"
60
+ kubectl delete secret -n sample-app-namespace --ignore-not-found $SECRET_NAME
61
+ echo "LOG: Creating secret for pulling sample app ECR"
62
+ kubectl create secret -n sample-app-namespace docker-registry $SECRET_NAME \
63
+ --docker-server=https://$ACCOUNT.dkr.ecr.${ var . aws_region } .amazonaws.com \
64
+ --docker-username=AWS \
65
+ --docker-password="$${TOKEN}"
65
66
66
67
# Deploy sample app
67
- " echo \ " LOG: Pulling sample app deployment files\" " ,
68
- " cd ~" , # To ensure everything is downloaded into root directory so cleanup is easy
69
- " aws s3api get-object --bucket aws-appsignals-sample-app-prod-us-east-1 --key frontend-service-depl.yaml frontend-service-depl.yaml" ,
70
- " aws s3api get-object --bucket aws-appsignals-sample-app-prod-us-east-1 --key remote-service-depl.yaml remote-service-depl.yaml" ,
71
- " echo \ " LOG: Applying sample app deployment files\" " ,
72
- " kubectl apply -f frontend-service-depl.yaml" ,
73
- " kubectl apply -f remote-service-depl.yaml" ,
68
+ echo "LOG: Pulling sample app deployment files"
69
+ cd ~", # To ensure everything is downloaded into root directory so cleanup is ea
70
+ aws s3api get-object --bucket aws-appsignals-sample-app-prod-us-east-1 --key frontend-service-depl.yaml frontend-service-depl.yaml
71
+ aws s3api get-object --bucket aws-appsignals-sample-app-prod-us-east-1 --key remote-service-depl.yaml remote-service-depl.yaml
72
+ echo "LOG: Applying sample app deployment files"
73
+ kubectl apply -f frontend-service-depl.yaml
74
+ kubectl apply -f remote-service-depl.yaml
74
75
75
76
# Expose sample app on port 30100
76
- " echo \ " LOG: Exposing main sample app on port 30100\" " ,
77
- " kubectl expose deployment sample-app-deployment-${ var . test_id } -n sample-app-namespace --type=\ " NodePort\ " --port 8080" ,
78
- " kubectl patch service sample-app-deployment-${ var . test_id } -n sample-app-namespace --type='json' --patch='[{\ " op\ " : \ " replace\ " , \ " path\ " : \ " /spec/ports/0/nodePort\ " , \ " value\ " :30100}]'" ,
77
+ echo "LOG: Exposing main sample app on port 30100"
78
+ kubectl expose deployment sample-app-deployment-${ var . test_id } -n sample-app-namespace --type="NodePort" --port 8080
79
+ kubectl patch service sample-app-deployment-${ var . test_id } -n sample-app-namespace --type='json' --patch='[{"op": "replace", "path": "/spec/ports/0/nodePort", "value":30100}]'
79
80
80
81
# Wait for sample app to be reach ready state
81
- " kubectl wait --for=condition=Ready --request-timeout '5m' pod --all -n sample-app-namespace" ,
82
+ kubectl wait --for=condition=Ready --request-timeout '5m' pod --all -n sample-app-namespace
82
83
83
84
# Emit remote service pod IP
84
- " echo \" LOG: Outputting remote service pod IP to SSM using put-parameter API\" " ,
85
- " aws ssm put-parameter --region ${ var . aws_region } --name remote-service-ip --type String --overwrite --value $(kubectl get pod --selector=app=remote-app -n sample-app-namespace -o jsonpath='{.items[0].status.podIP}')" ,
85
+ echo "LOG: Outputting remote service pod IP to SSM using put-parameter API"
86
+ aws ssm put-parameter --region ${ var . aws_region } --name remote-service-ip --type String --overwrite --value $(kubectl get pod --selector=app=remote-app -n sample-app-namespace -o jsonpath='{.items[0].status.podIP}')
87
+
88
+ EOF
86
89
]
87
90
}
88
91
}
0 commit comments