Skip to content

Commit 662fd43

Browse files
authored
Merge pull request #10 from oracle-devrel/terraform
config with terraform
2 parents 3d83b09 + 64a5c62 commit 662fd43

33 files changed

+381
-0
lines changed

oci-config-examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All about OCI devops deployment samples ..
55
<summary>Policies & Groups - click to expand</summary>
66

77
* [Devops related policies and groups](./oci-devops-policies-groups/)
8+
* [OCI Policy management using terraform](./oci_devops_policy_dg_terraform/)
89

910
</details>
1011

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
2+
Manage OCI policies using a OCI Devops build pipeline and OCI Terraform.
3+
------
4+
5+
### Objectives
6+
7+
- Manage policies and dynamic group and more using OCI Devops and Terraform.
8+
- Demonstrate how to execute terraform scripts from OCI Build runner.
9+
- Manage Terraform state management using object storage.
10+
11+
*Target Audience* : OCI Intermediate and Experienced
12+
13+
### Flow
14+
15+
![](images/oci_devops_policies_dgs.drawio.png)
16+
17+
* Specific instruction to clone only this example.
18+
19+
```
20+
$ git init oci_devops_policy_dg_terraform
21+
$ cd oci_devops_policy_dg_terraform
22+
$ git remote add origin https://github.com/oracle-devrel/oci-devops-examples
23+
$ git config core.sparsecheckout true
24+
$ echo "oci-config-examples/oci_devops_policy_dg_terraform/*">>.git/info/sparse-checkout
25+
$ git pull --depth=1 origin main
26+
27+
```
28+
29+
### Procedure
30+
31+
- Its a specific sample with admin/root privilege ,so ensure that you are an admin to the tenancy or assigned with administrator privileges by the tenancy admin.
32+
33+
- Create an OCI Notification topic - https://docs.oracle.com/en-us/iaas/Content/Notification/home.htm
34+
35+
![](images/oci_topic.png)
36+
37+
- Create a dynamic group and add below rules.
38+
39+
```
40+
ALL {resource.type = 'devopsbuildpipeline', resource.compartment.id = 'ocid1.compartment.oc1..xxx'}
41+
ALL {resource.type = 'devopsrepository', resource.compartment.id = 'ocid1.compartment.oc1..xxx'}
42+
43+
```
44+
45+
- Create a policy with below statements.
46+
47+
```
48+
Allow dynamic-group "<DG Name>" to manage repos in compartment "<Compartment Name>"
49+
Allow dynamic-group "<DG Name>" to read secret-family in compartment "<Compartment Name>"
50+
Allow dynamic-group "<DG Name>" to manage devops-family in compartment "<Compartment Name>"
51+
Allow dynamic-group "<DG Name>" to manage generic-artifacts in compartment "<Compartment Name>"
52+
Allow dynamic-group "<DG Name>" to use ons-topics in compartment "<Compartment Name>"
53+
Allow dynamic-group "<DG Name>" to read secret-family in compartment "<Compartment Name>"
54+
```
55+
56+
- Create a devops project - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_project.htm#create_a_project
57+
- Associate it with the notification topic created.
58+
59+
![](images/oci_project.png)
60+
61+
- Enables logs for projects.
62+
63+
![](images/oci_logs.png)
64+
65+
- Created a code repo - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_repo.htm#create_repo
66+
67+
![](images/oci_repo.png)
68+
69+
- Create a build pipeline - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_buildpipeline.htm#create_buildpipeline
70+
71+
![](images/oci_buildpipeline.png)
72+
73+
- Add a managed build stage to the build pipeline - https://docs.oracle.com/en-us/iaas/Content/devops/using/add_buildstage.htm
74+
75+
![](images/oci_buildstage_1.png)
76+
77+
![](images/oci_buildstage_2.png)
78+
79+
- Create an object storage bucket - https://docs.oracle.com/en-us/iaas/Content/Object/home.htm
80+
81+
![](images/oci_objectstore.png)
82+
83+
- Create a customer secret keys for the user - https://docs.oracle.com/en-us/iaas/Content/Identity/Tasks/managingcredentials.htm#Working2
84+
85+
![](images/oci_user_1.png)
86+
87+
- Make a note of the Access key and Password info.
88+
89+
![](images/oci_user_2.png)
90+
91+
- Create an OCI Vault and create a master key - https://docs.oracle.com/en-us/iaas/Content/KeyManagement/home.htm
92+
93+
![](images/oci_vault_1.png)
94+
95+
- Create two secretes and store customer secret keys and password.
96+
97+
![](images/oci_vault_2.png)
98+
99+
- Now that we have all the infrastructure and services are ready, let us do an implementation .
100+
- Clone the repo to local machine or cloud -shell.
101+
- Let us check the folder and file tree.
102+
103+
```
104+
terraform/compartment_scope: <For all terraform definitions need to implement with in individual compartment.
105+
total 16
106+
-rw-r--r--. 1 rahul oci 566 Apr 21 12:31 policies.tf
107+
-rw-r--r--. 1 rahul oci 102 Apr 21 12:31 provider.tf
108+
-rw-r--r--. 1 rahul oci 471 Apr 21 12:31 remote-backend_template.tf
109+
-rw-r--r--. 1 rahul oci 452 Apr 21 12:31 variables.tf
110+
111+
terraform/tenancy_scope: <For all terraform definitions need to implement under root of the tenancy.
112+
total 20
113+
-rw-r--r--. 1 rahul oci 360 Apr 21 12:31 dgs.tf
114+
-rw-r--r--. 1 rahul oci 102 Apr 21 12:31 provider.tf
115+
-rw-r--r--. 1 rahul oci 476 Apr 21 12:31 remote-backend_template.tf
116+
-rw-r--r--. 1 rahul oci 459 Apr 21 12:31 remote-backend.tf
117+
-rw-r--r--. 1 rahul oci 427 Apr 21 12:31 variables.tf
118+
```
119+
120+
- For sample run we will be adding a dynamic group(under root of the tenancy) and policy per compartment.
121+
122+
- Add the necessary compartment informations under `compartments.config`.
123+
- The format should be "compartment name=compartment ocid"
124+
125+
![](images/oci_compartment_ids.png)
126+
127+
- Update the values for scope compartment.
128+
129+
```
130+
$ cd terraform/compartment_scope
131+
$ Edit variables.tf and update the tenancy_ocid
132+
$ Edit remote-backend_template.tf and update the endpoint.
133+
```
134+
- Endpoint format is "https://NAMESPACEcompat.objectstorage.OCIREGION.oraclecloud.com"
135+
136+
- Update the values for scope tenancy(root).
137+
138+
```
139+
$ cd ../tenancy_scope
140+
$ Edit variables.tf and update tenancy_ocid and region values.
141+
$ Edit remote-backend_template.tf and update the endpoint.
142+
```
143+
144+
- Push the change back to OCI Code repo using using GIT CLI or UI over https or ssh authorization towards OCI Code repo - https://docs.oracle.com/en-us/iaas/Content/devops/using/clone_repo.htm
145+
146+
![](images/oci_coderepo.png)
147+
148+
- Let us do a manual build run.
149+
150+
![](images/oci_build_run_1.png)
151+
152+
- Wait for the completion of the build run .
153+
154+
![](images/oci_build_run_2.png)
155+
156+
- Re verify the implementation of tenancy level . Switch to dynamic groups and we should see two group with a compartment name post fixed.
157+
158+
![](images/oci_dg.png)
159+
160+
- Switch to policies and switch the different compartment mentioned and validate the policies.We should see one policy per compartment added as per the execution.
161+
162+
- Verify the terraform backend states via object storage files.
163+
- You should see even number files ,half with a prefix of `root_` and other with name of the compartment.
164+
- The files with root_ stores the state of resources that are created under tenancy root for a specific compartment
165+
166+
![](images/oci_objects_1.png)
167+
168+
![](images/oci_objects_2.png)
169+
170+
- You may add /update terraform definitions and explore options on state consistency (like a manual deletion or re-run with state and resources intact.)
171+
172+
Read more
173+
----
174+
175+
- OCI Devops - https://docs.oracle.com/en-us/iaas/Content/devops/using/home.htm.
176+
- OCI Reference architectures - https://docs.oracle.com/solutions/
177+
- OCI Devops samples - https://github.com/oracle-devrel/oci-devops-examples
178+
179+
180+
Contributors
181+
===========
182+
183+
- Author : Rahul M R.
184+
- Colloboroators : NA
185+
- Last release : April 2022
186+
187+
188+
### Back to examples.
189+
----
190+
191+
- 🍿 [Back to OCI Devops Config sample](./../README.md)
192+
- 🏝️ [Back to OCI Devops sample](./../../README.md)
193+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
version: 0.1
2+
component: build
3+
timeoutInSeconds: 6000
4+
runAs: root
5+
shell: bash
6+
env:
7+
# these are local variables to the build config
8+
variables:
9+
region: "us-ashburn-1" # Set your OCI Region.
10+
11+
# the value of a vaultVariable is the secret-id (in OCI ID format) stored in the OCI Vault service
12+
# you can then access the value of that secret in your build_spec.yaml commands
13+
vaultVariables:
14+
aws_access_key_id: ocid1.vaultsecret.oc1.iad.xxxx
15+
aws_secret_access_key: ocid1.vaultsecret.oc1.iad.yyyy
16+
17+
18+
19+
steps:
20+
- type: Command
21+
name: "Install Terraform"
22+
timeoutInSeconds: 400
23+
command: |
24+
yum install -y yum-utils
25+
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
26+
yum -y install terraform
27+
28+
29+
- type: Command
30+
name: "TENANCY - Terraform actions"
31+
timeoutInSeconds: 400
32+
command: |
33+
cd ${OCI_PRIMARY_SOURCE_DIR}/terraform
34+
for cname in `cat ${OCI_PRIMARY_SOURCE_DIR}/compartments.config|sort -u -k 1|cut -d = -f 1`
35+
do
36+
echo "Acting on ROOT Tenancy for Compartment - ${cname}.."
37+
mkdir ${cname}
38+
cp -r tenancy_scope/* ${cname}/
39+
cd ${cname}
40+
export compartment_name=${cname}
41+
echo "[default]" > cred_store
42+
echo "aws_access_key_id=${aws_access_key_id}">>cred_store
43+
echo "aws_secret_access_key=${aws_secret_access_key}">>cred_store
44+
cat remote-backend_template.tf|envsubst > remote-backend.tf
45+
rm remote-backend_template.tf
46+
export TF_VAR_compartment_name=${cname}
47+
export TF_VAR_compartment_ocid=`cat ${OCI_PRIMARY_SOURCE_DIR}/compartments.config|grep ${cname}|cut -f 2 -d '='`
48+
terraform version
49+
terraform init -migrate-state
50+
terraform plan -out plan.out
51+
export TF_LOG=debug
52+
terraform apply plan.out
53+
cd ..
54+
rm -r ${cname}
55+
done
56+
57+
- type: Command
58+
name: "COMPARTMENT - Terraform actions"
59+
timeoutInSeconds: 400
60+
command: |
61+
cd ${OCI_PRIMARY_SOURCE_DIR}/terraform
62+
for cname in `cat ${OCI_PRIMARY_SOURCE_DIR}/compartments.config|sort -u -k 1|cut -d = -f 1`
63+
do
64+
echo "Acting directly on Compartment - ${cname}.."
65+
mkdir ${cname}
66+
cp -r compartment_scope/* ${cname}/
67+
cd ${cname}
68+
export compartment_name=${cname}
69+
echo "[default]" > cred_store
70+
echo "aws_access_key_id=${aws_access_key_id}">>cred_store
71+
echo "aws_secret_access_key=${aws_secret_access_key}">>cred_store
72+
cat remote-backend_template.tf|envsubst > remote-backend.tf
73+
rm remote-backend_template.tf
74+
export TF_VAR_compartment_name=${cname}
75+
export TF_VAR_compartment_ocid=`cat ${OCI_PRIMARY_SOURCE_DIR}/compartments.config|grep ${cname}|cut -f 2 -d '='`
76+
terraform version
77+
terraform init -migrate-state
78+
terraform plan -out plan.out
79+
terraform apply plan.out
80+
cd ..
81+
rm -r ${cname}
82+
done
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mr-prod-compartment=ocid1.compartment.oc1..xxxx
2+
mr-devops-canary=ocid1.compartment.oc1..fffff
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
**DS_Store
166 KB
Loading
401 KB
Loading
37.7 KB
Loading
91.9 KB
Loading
101 KB
Loading

0 commit comments

Comments
 (0)