1
0
mirror of https://github.com/tektoncd/catalog.git synced 2024-11-23 06:08:46 +00:00

adding python boto3 task

This commit is contained in:
arunvel1988 2024-06-23 08:45:15 +05:30 committed by tekton-robot
parent fe39623ded
commit 8f256824e0
5 changed files with 146 additions and 0 deletions

View File

@ -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).

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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