From 16205e55438f7e11e8a82fa4de7dfd61939e7c56 Mon Sep 17 00:00:00 2001 From: arunvel1988 Date: Sun, 23 Jun 2024 08:45:15 +0530 Subject: [PATCH] adding python boto3 task --- task/python-boto3-aws/0.1/README.md | 58 +++++++++++++++++++ .../0.1/python-boto3-aws.yaml | 44 ++++++++++++++ .../0.1/samples/config-map.yaml | 14 +++++ task/python-boto3-aws/0.1/samples/run.yaml | 10 ++++ task/python-boto3-aws/0.1/samples/secret.yaml | 20 +++++++ 5 files changed, 146 insertions(+) create mode 100644 task/python-boto3-aws/0.1/README.md create mode 100644 task/python-boto3-aws/0.1/python-boto3-aws.yaml create mode 100644 task/python-boto3-aws/0.1/samples/config-map.yaml create mode 100644 task/python-boto3-aws/0.1/samples/run.yaml create mode 100644 task/python-boto3-aws/0.1/samples/secret.yaml diff --git a/task/python-boto3-aws/0.1/README.md b/task/python-boto3-aws/0.1/README.md new file mode 100644 index 0000000000..2752fd6563 --- /dev/null +++ b/task/python-boto3-aws/0.1/README.md @@ -0,0 +1,58 @@ +# Python Boto3 Task + +This Tekton Task defines a reusable Task named `python-boto3` for running Python scripts that use the boto3 library to interact with AWS services. + +## Parameters + +The Task accepts the following parameters: + +- `aws-region` (optional, default: `us-east-1`): The AWS region to use for the boto3 client. + +## Volumes + +The Task expects a ConfigMap named `python-script-configmap` to be mounted as a volume named `python-script`. This ConfigMap should contain the Python script to be executed, with the key `script.py`. + +## Steps + +The Task consists of a single step that runs the Python script using the `python:3.9` image. The step performs the following actions: + +1. Installs the `boto3` library using `pip`. +2. Sets the AWS credentials as environment variables (`AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`) from a Kubernetes Secret named `aws-credentials`. +3. Sets the AWS region as an environment variable (`AWS_DEFAULT_REGION`) using the value provided in the `aws-region` parameter. +4. Mounts the `python-script` volume containing the Python script at `/workspace/python-script`. +5. Executes the Python script located at `/workspace/python-script/script.py`. + +## Usage + +To use this Task, you'll need to create the following resources: + +1. A Kubernetes Secret named `aws-credentials` with your AWS Access Key ID and Secret Access Key. +2. A ConfigMap named `python-script-configmap` with your Python script (`script.py`). + + Python script named script.py with the following content: + +##################################################### +# script.py - begin +##################################################### +import boto3 + +# Your Python script that uses boto3 goes here +# For example: +s3 = boto3.client('s3') +response = s3.list_buckets() + +print(response) + +################################################### +# script.py - end +################################################### +To create the ConfigMap, you can use the kubectl create configmap command and specify the --from-file flag to include the Python script file: +###### +# command to create configmap +##### +kubectl create configmap python-script-configmap --from-file=script.py +This command will create a ConfigMap named python-script-configmap with the contents of the script.py file. +Alternatively you can also use config-map.yaml given in sample + +Then, you can create a Tekton TaskRun that references this Task and provide the necessary parameters (if any). + diff --git a/task/python-boto3-aws/0.1/python-boto3-aws.yaml b/task/python-boto3-aws/0.1/python-boto3-aws.yaml new file mode 100644 index 0000000000..62caeb8750 --- /dev/null +++ b/task/python-boto3-aws/0.1/python-boto3-aws.yaml @@ -0,0 +1,44 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: python-boto3 + labels: + app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.54.0" + tekton.dev/categories: sdk + tekton.dev/tags: CLI, boto3, sdk + tekton.dev/displayName: "python boto3 aws" + tekton.dev/platforms: "linux/amd64" +spec: + params: + - name: aws-region + type: string + default: "us-east-1" + description: AWS Region + volumes: + - name: python-script + configMap: + name: python-script-configmap + steps: + - name: run-python-script + image: docker.io/library/python:3.9.19-alpine3.20@sha256:45cc18540209d878c2b24080cf8f64fc37603721b67d0ecc508799e2f9a9b21d + env: + - name: AWS_ACCESS_KEY_ID + valueFrom: + secretKeyRef: + name: aws-credentials + key: access-key-id + - name: AWS_SECRET_ACCESS_KEY + valueFrom: + secretKeyRef: + name: aws-credentials + key: secret-access-key + - name: AWS_DEFAULT_REGION + value: $(params.aws-region) + volumeMounts: + - name: python-script + mountPath: /workspace/python-script + script: | + pip install boto3 + python /workspace/python-script/script.py \ No newline at end of file diff --git a/task/python-boto3-aws/0.1/samples/config-map.yaml b/task/python-boto3-aws/0.1/samples/config-map.yaml new file mode 100644 index 0000000000..560c1587ef --- /dev/null +++ b/task/python-boto3-aws/0.1/samples/config-map.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: python-script-configmap +data: + script.py: | + import boto3 + + # Your Python script that uses boto3 goes here + # For example: + s3 = boto3.client('s3') + response = s3.list_buckets() + + print(response) \ No newline at end of file diff --git a/task/python-boto3-aws/0.1/samples/run.yaml b/task/python-boto3-aws/0.1/samples/run.yaml new file mode 100644 index 0000000000..c75055edb6 --- /dev/null +++ b/task/python-boto3-aws/0.1/samples/run.yaml @@ -0,0 +1,10 @@ +apiVersion: tekton.dev/v1beta1 +kind: TaskRun +metadata: + name: python-boto3-run +spec: + taskRef: + name: python-boto3 + params: + - name: aws-region + value: us-west-2 \ No newline at end of file diff --git a/task/python-boto3-aws/0.1/samples/secret.yaml b/task/python-boto3-aws/0.1/samples/secret.yaml new file mode 100644 index 0000000000..d940c24bf9 --- /dev/null +++ b/task/python-boto3-aws/0.1/samples/secret.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Secret +metadata: + name: aws-credentials +type: Opaque +stringData: + credentials: |- + [$(profile-name)] + aws_access_key_id = $(aws_access_key_id) + aws_secret_access_key = $(aws_secret_access_key) + + [default] + aws_access_key_id = $(aws_access_key_id) + aws_secret_access_key = $(aws_secret_access_key) + config: |- + [profile $(profile-name)] + region = us-east-1 + output = text + [default] + region = us-east-2 \ No newline at end of file