You are looking at the documentation of a prior release. To read the documentation of the latest release, please
visit here.
This guide will show you how to provision (Create, Update, Delete) an AWS S3 Bucket using Kubeform.
Examples used in this guide can be found here.
At first, let’s look at the Terraform
configuration for an AWS S3 Bucket below:
provider "aws" {
access_key = "ACCESS_KEY"
region = "REGION_NAME"
secret_key = "SECRET_KEY"
}
resource "aws_s3_bucket" "test1" {
bucket = "s3-bucket-test1"
}
Now, if we apply terraform apply
this config will create an AWS S3 Bucket. We’ll create the exact configuration using kubeform
. The steps are given below:
At first, create the CRD of AWS S3 Bucket using the following kubectl command:
$ kubectl apply -f https://raw.githubusercontent.com/kubeform/provider-aws-api/master/crds/s3.aws.kubeform.com_buckets.yaml
Then create the secret which is necessary for provisioning the S3 Bucket in AWS.
apiVersion: v1
kind: Secret
metadata:
name: aws-provider-secret
stringData:
provider: |
{
"region": "<REGION_NAME>",
"access_key": "<ACCESS_KEY>",
"secret_key": "<SECRET_KEY>"
}
Here we can see that, the provider
field of the stringData
of the secret is same as the field of the provider part in the terraform config file. The provider secret needs to be provided in json format, under the provider
key. Save it in a file (eg. provider-secret.yaml
) then apply it using kubectl.
$ kubectl apply -f provider-secret.yaml
Note: Here, key of the provider field of the stringData (eg.
"access_key"
,"secret_key"
) must be in snake case format (same as the tf configuration file)
Now, we’ll create the S3 Bucket CRD. The yaml is given below:
apiVersion: s3.aws.kubeform.com/v1alpha1
kind: Bucket
metadata:
name: test1
spec:
resource:
bucket: s3-bucket-test1
providerRef:
name: aws-provider-secret
terminationPolicy: DoNotTerminate
Here, the resource
field contains the AWS S3 Bucket resource spec. Also, we can see that the provider secret is referenced using a field called providerRef
.
We can see a field named
terminationPolicy
, this is a feature of kubeform. This field can have two values,Delete
orDoNotTerminate
. When the value of this field is set toDoNotTerminate
then the resource won’t get deleted even though we applykubectl delete
operation, this field needs to be set toDelete
to delete the resource. It helps to avoid accidental deletion of the resource. We will see the use of this field inDelete S3 Bucket
part later on this guide.
Save it in a file (eg. aws-s3-bucket.yaml
) then apply it using kubectl.
$ kubectl apply -f aws-s3-bucket.yaml
After applying this command, the resource will be in InProgress
phase until the cloud creates the resource. Once the cloud resource get created, the resource will be in Current
phase which means we have successfully created the resource.
After successful creation of the resource, the resource state is available under spec.state
section. This spec.state
field maps the real world resource to the kubeform resource. This field doesn’t contain any sensitive field. Sensitive fields are stored in the secret specified in the spec.secretRef
section. If no secretRef
is specified, kubeform will create one.
Now, we’ll update the S3 Bucket CRD. For updating the Bucket, we will modify the existing aws-s3-bucket.yaml
, we will use a different bucket
(s3-bucket-test1-update
) field. The modified yaml is given below:
apiVersion: s3.aws.kubeform.com/v1alpha1
kind: Bucket
metadata:
name: test1
spec:
resource:
bucket: s3-bucket-test1-update
providerRef:
name: aws-provider-secret
terminationPolicy: DoNotTerminate
Now, apply it using kubectl command.
$ kubectl apply -f aws-s3-bucket.yaml
After that, existing AWS S3 Bucket will be updated!
Note: Here, we have changed the
bucket
field which is Immutable, means if we change an immutable field then the resource will first get deleted and then created. But, there are some fields which are mutable, means changing those fields, resource will be only updated/changed. So, be careful!
To delete the S3 Bucket just run:
$ kubectl delete -f aws-s3-bucket.yaml
After applying this command we will get below error message, as we have set terminationPolicy: DoNotTerminate
:
Error from server (bucket "default/test1" can't be terminated. To delete, change spec.terminationPolicy to Delete): error when deleting "aws-s3-bucket.yaml": admission webhook "bucket.s3.aws.kubeform.com" denied the request: bucket "default/test1" can't be terminated. To delete, change spec.terminationPolicy to Delete
Let’s change the terminationPolicy
to Delete
by using kubectl patch command.
$ kubectl patch -n default bucket test1 -p '{"spec":{"terminationPolicy":"Delete"}}' --type="merge"
Now, we can delete the Bucket.
$ kubectl delete -f aws-s3-bucket.yaml
After applying this command the resource will be in Terminating
phase until the cloud resource get destroyed. Once the cloud resource get destroyed, the resource will get deleted successfully.