AWS Quickstart Part 3

This document is for an older version of Crossplane.

This document applies to Crossplane version v1.11 and not to the latest release v1.13.

Important

This guide is part 3 of a series.

Follow part 1 to install Crossplane and connect your Kubernetes cluster to AWS.

Follow part 2 to create a composition, custom resource definition and a claim.

Part 2 created a composite resource definition to define the schema of the custom API. Users create a claim to use the custom API and apply their options. Part 2 didn’t show how the options set in a claim change or get applied the associated composite resources.

Prerequisites

  • Complete quickstart part 1 and Part 2 to install Crossplane and the quickstart configurations.

  1. Add the Crossplane Helm repository and install Crossplane
1helm repo add \
2crossplane-stable https://charts.crossplane.io/stable
3helm repo update
4&&
5helm install crossplane \
6crossplane-stable/crossplane \
7--namespace crossplane-system \
8--create-namespace
  1. When the Crossplane pods finish installing and are ready, apply the AWS Provider
1cat <<EOF | kubectl apply -f -
2apiVersion: pkg.crossplane.io/v1
3kind: Provider
4metadata:
5  name: upbound-provider-aws
6spec:
7  package: xpkg.upbound.io/upbound/provider-aws:v0.27.0
8EOF
  1. Create a file called aws-credentials.txt with your AWS keys
1[default]
2aws_access_key_id = 
3aws_secret_access_key = 
  1. Create a Kubernetes secret from the AWS keys
1kubectl create secret \
2generic aws-secret \
3-n crossplane-system \
4--from-file=creds=./aws-credentials.txt
  1. Create a ProviderConfig
 1cat <<EOF | kubectl apply -f -
 2apiVersion: aws.upbound.io/v1beta1
 3kind: ProviderConfig
 4metadata:
 5  name: default
 6spec:
 7  credentials:
 8    source: Secret
 9    secretRef:
10      namespace: crossplane-system
11      name: aws-secret
12      key: creds
13EOF
  1. Create a composition
 1cat <<EOF | kubectl apply -f -
 2apiVersion: apiextensions.crossplane.io/v1
 3kind: Composition
 4metadata:
 5  name: dynamodb-with-bucket
 6spec:
 7  compositeTypeRef:
 8    apiVersion: custom-api.example.org/v1alpha1
 9    kind: XDatabase
10  resources:
11    - name: s3-bucket
12      base:
13        apiVersion: s3.aws.upbound.io/v1beta1
14        kind: Bucket
15        spec:
16          forProvider:
17            region: "us-east-2"
18    - name: dynamodb
19      base:
20        apiVersion: dynamodb.aws.upbound.io/v1beta1
21        kind: Table
22        spec:
23          forProvider:
24            region: "us-east-2"
25            writeCapacity: 1
26            readCapacity: 1
27            attribute:
28              - name: S3ID
29                type: S
30            hashKey: S3ID
31EOF
  1. Create a composite resource definition
 1cat <<EOF | kubectl apply -f -
 2apiVersion: apiextensions.crossplane.io/v1
 3kind: CompositeResourceDefinition
 4metadata:
 5  name: xdatabases.custom-api.example.org
 6spec:
 7  group: custom-api.example.org
 8  names:
 9    kind: XDatabase
10    plural: xdatabases
11  versions:
12  - name: v1alpha1
13    served: true
14    referenceable: true
15    schema:
16      openAPIV3Schema:
17        type: object
18        properties:
19          spec:
20            type: object
21            properties:
22              region:
23                type: string
24                oneOf:
25                  - pattern: '^EU$'
26                  - pattern: '^US$'
27            required:
28              - region
29  claimNames:
30    kind: Database
31    plural: databases
32EOF
  1. Create a new namespace
1kubectl create namespace test

Enable composition patches

In a composition patches map fields in the custom API to fields inside the managed resources.

The example composition has two managed resources, a bucket and a table.

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3# Removed for Brevity
 4resources:
 5    - name: s3-bucket
 6      base:
 7        apiVersion: s3.aws.upbound.io/v1beta1
 8        kind: Bucket
 9        spec:
10          forProvider:
11            region: "us-east-2"
12    - name: dynamodb
13      base:
14        apiVersion: dynamodb.aws.upbound.io/v1beta1
15        kind: Table
16        spec:
17          forProvider:
18            region: "us-east-2"
19            writeCapacity: 1
20            readCapacity: 1
21            attribute:
22              - name: S3ID
23                type: S
24            hashKey: S3ID

The custom API defined a single option, region. A region can be either EU or US.

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: CompositeResourceDefinition
 3# Removed for brevity
 4spec:
 5  group: custom-api.example.org
 6  names:
 7    kind: XDatabase
 8# Removed for brevity
 9      spec:
10        type: object
11        properties:
12          region:
13            type: string
14            oneOf:
15              - pattern: '^EU$'
16              - pattern: '^US$'

Creating a composition patch allows Crossplane to update the settings of the composite resource. Patches apply to the individual managed resources inside the composition.

A patch has a fromField and a toField specifying which value from the custom API should apply to the managed resource.
Patches can create a transform to change the from field before it’s applied.

The transform type is what kind of change to make on the from field. Types of changes could include appending a string, preforming a math operation or mapping one value to another.

Applying a patch to the Bucket uses the custom API region to use as the managed resource region.

The custom API value “EU” is mapped to the value “eu-north-1” and “US” is mapped to the value “us-east-2.”

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3# Removed for Brevity
 4resources:
 5    - name: s3-bucket
 6      base:
 7        apiVersion: s3.aws.upbound.io/v1beta1
 8        kind: Bucket
 9        spec:
10          forProvider:
11            region: "us-east-2"
12      patches:
13        - fromFieldPath: "region"
14          toFieldPath: "spec.forProvider.region"
15          transforms:
16            - type: map
17              map: 
18                EU: "eu-north-1"
19                US: "us-east-2"

Patching is a powerful tool enabling simpler or abstracted APIs. A developer isn’t required to know the specific AWS region identifier, only the abstracted option of “EU” or “US.”

Apply the updated composition

Apply the same patch to the Table managed resource and apply the updated composition.

 1cat <<EOF | kubectl apply -f -
 2apiVersion: apiextensions.crossplane.io/v1
 3kind: Composition
 4metadata:
 5  name: dynamodb-with-bucket
 6spec:
 7  compositeTypeRef:
 8    apiVersion: custom-api.example.org/v1alpha1
 9    kind: XDatabase
10  resources:
11    - name: s3-bucket
12      base:
13        apiVersion: s3.aws.upbound.io/v1beta1
14        kind: Bucket
15        spec:
16          forProvider:
17            name: default
18            region: "us-east-2"
19      patches:
20        - fromFieldPath: "spec.region"
21          toFieldPath: "spec.forProvider.region"
22          transforms:
23            - type: map
24              map: 
25                EU: "eu-north-1"
26                US: "us-east-2"
27    - name: dynamodb
28      base:
29        apiVersion: dynamodb.aws.upbound.io/v1beta1
30        kind: Table
31        spec:
32          forProvider:
33            writeCapacity: 1
34            readCapacity: 1
35            attribute:
36              - name: S3ID
37                type: S
38            hashKey: S3ID
39            region: "us-east-2"
40      patches:
41        - fromFieldPath: "spec.region"
42          toFieldPath: "spec.forProvider.region"
43          transforms:
44            - type: map
45              map: 
46                EU: "eu-north-1"
47                US: "us-east-2"
48EOF

Create a claim

Create a new claim and set the region to “EU.”

1cat <<EOF | kubectl apply -f -
2apiVersion: custom-api.example.org/v1alpha1
3kind: Database
4metadata:
5  name: claimed-eu-database
6  namespace: test
7spec:
8  region: "EU"
9EOF

View the claim with kubectl get claim

1kubectl get database -n test
2NAME                  SYNCED   READY   CONNECTION-SECRET   AGE
3claimed-eu-database   True     True                        18m

The claim reports SYNCED and READY as True after Crossplane creates all the managed resources.

Describe the Table resource to see the AWS region is eu-north-1.

1kubectl describe table | grep arn:aws
2    Arn:           arn:aws:dynamodb:eu-north-1:622343227358:table/claimed-eu-database-2sh9w-dhvw6

Using region: "EU" patches the composite resource, updating the AWS region from us-east-2 to eu-north-1. The developer creating the claim isn’t required to know which specific AWS region or the naming conventions. Using the abstract API options of “EU” or “US” the developer places their resources in the desired location.

Deleting the claim removes the managed resources.

Note
The managed resources take up to 5 minutes to delete.
1kubectl delete database claimed-eu-database -n test

Create a Crossplane configuration package

Crossplane configuration packages allow users to combine their custom resource definition and composition files into an OCI image.

Note
The Open Container Initiative defines the OCI image standard.
An OCI images is a standard way to package data.

You can host configuration packages in image registries like Docker Hub or the Upbound Marketplace.

Crossplane can download and install configuration packages into a Kubernetes cluster.

Creating a configuration package makes your Crossplane custom APIs portable and versioned.

Building and installing configuration packages requires an OCI image compatible tool.

Note
You can use any software that builds OCI images. This includes Docker or Upbound’s Up CLI).

A configuration package includes three files:

  • crossplane.yaml defines the metadata of the package.
  • definition.yaml is the composite resource definition for the package.
  • composition.yaml is the composition template for the package.

Create a crossplane.yaml file

Configuration packages describe their contents and requirements with a crossplane.yaml file.

The crossplane.yaml file lists the required Crossplane providers and their compatible versions as well as the required Crossplane version.

The Crossplane meta.pkg API defines the schema for a Configuration.

Inside the spec define the required Crossplane version.

The dependsOn section lists the dependencies for a package.

This package lists the Upbound provider-aws version 0.27.0 or later as a dependency.

Tip
Crossplane automatically installs dependencies. Dependencies can include other configuration packages.
 1apiVersion: meta.pkg.crossplane.io/v1
 2kind: Configuration
 3metadata:
 4  name: crossplane-aws-quickstart
 5spec:
 6  crossplane:
 7    version: ">=v1.11.0"
 8  dependsOn:
 9    - provider: xpkg.upbound.io/upbound/provider-aws
10      version: ">=v0.27.0"

Create a new directory and save the crossplane.yaml file.

 1mkdir crossplane-aws-quickstart
 2cat <<EOF > crossplane-aws-quickstart/crossplane.yaml
 3apiVersion: meta.pkg.crossplane.io/v1
 4kind: Configuration
 5metadata:
 6  name: crossplane-aws-quickstart
 7spec:
 8  crossplane:
 9    version: ">=v1.11.0"
10  dependsOn:
11    - provider: xpkg.upbound.io/upbound/provider-aws
12      version: ">=v0.27.0"
13EOF

Create a definition.yaml file

A configuration package requires a composite resource definition (XRD) to define the custom API.

Save the XRD as definition.yaml in the same directory as the crossplane.yaml file.

 1cat <<EOF > crossplane-aws-quickstart/definition.yaml
 2apiVersion: apiextensions.crossplane.io/v1
 3kind: CompositeResourceDefinition
 4metadata:
 5  name: xdatabases.custom-api.example.org
 6spec:
 7  group: custom-api.example.org
 8  names:
 9    kind: XDatabase
10    plural: xdatabases
11  versions:
12  - name: v1alpha1
13    served: true
14    referenceable: true
15    schema:
16      openAPIV3Schema:
17        type: object
18        properties:
19          spec:
20            type: object
21            properties:
22              region:
23                type: string
24                oneOf:
25                  - pattern: '^EU$'
26                  - pattern: '^US$'
27            required:
28              - region
29  claimNames:
30    kind: Database
31    plural: databases
32EOF

Create a composition.yaml file

The composition template creates the managed resources and allows patches to customize the managed resources.

Copy the composition into the composition.yaml file in the same directory as crossplane.yaml.

 1cat <<EOF > crossplane-aws-quickstart/composition.yaml
 2apiVersion: apiextensions.crossplane.io/v1
 3kind: Composition
 4metadata:
 5  name: dynamodb-with-bucket
 6spec:
 7  compositeTypeRef:
 8    apiVersion: custom-api.example.org/v1alpha1
 9    kind: XDatabase
10  resources:
11    - name: s3-bucket
12      base:
13        apiVersion: s3.aws.upbound.io/v1beta1
14        kind: Bucket
15        spec:
16          providerConfigRef:
17            name: default
18        patches:
19        - fromFieldPath: "spec.region"
20          toFieldPath: "spec.forProvider.region"
21          transforms:
22             - type: map
23               map: 
24                EU: "eu-north-1"
25                US: "us-east-1"
26    - name: dynamodb
27      base:
28        apiVersion: dynamodb.aws.upbound.io/v1beta1
29        kind: Table
30        spec:
31          forProvider:
32            writeCapacity: 1
33            readCapacity: 1
34            attribute:
35              - name: S3ID
36                type: S
37            hashKey: S3ID
38      patches:
39        - fromFieldPath: "spec.region"
40          toFieldPath: "spec.forProvider.region"
41          transforms:
42            - type: map
43              map: 
44                EU: "eu-north-1"
45                US: "us-east-1"
46EOF

Install the Crossplane command-line

To build a configuration package install the Crossplane Kubernetes command-line extension.

1wget "https://raw.githubusercontent.com/crossplane/crossplane/master/install.sh"
2chmod +x install.sh
3./install.sh
4sudo mv crossplane /usr/local/bin

Verify the Crossplane command-line installed with crossplane --help

 1crossplane --help
 2Usage: crossplane <command>
 3
 4A command line tool for interacting with Crossplane.
 5
 6Flags:
 7  -h, --help       Show context-sensitive help.
 8  -v, --version    Print version and quit.
 9      --verbose    Print verbose logging statements.
10# Ouptut removed for brevity

Build a configuration package

Use the crossplane CLI to create an .xpkg file containing the custom APIs and Crossplane configuration.

1crossplane build configuration -f crossplane-aws-quickstart/ --name="crossplane-aws-quickstart"

Now an .xpkg OCI image is inside the crossplane-aws-quickstart directory.

1ls crossplane-aws-quickstart/
2composition.yaml  crossplane-aws-quickstart.xpkg  crossplane.yaml  definition.yaml

Next steps