Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding python boto3 task #1283

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions task/python-boto3-aws/0.1/README.md
Original file line number Diff line number Diff line change
@@ -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).

44 changes: 44 additions & 0 deletions task/python-boto3-aws/0.1/python-boto3-aws.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions task/python-boto3-aws/0.1/samples/config-map.yaml
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions task/python-boto3-aws/0.1/samples/run.yaml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions task/python-boto3-aws/0.1/samples/secret.yaml
Original file line number Diff line number Diff line change
@@ -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