[AWS] Using ini files with Lambda [Python]

Contents

It is a small story. I think that there are many times when I am writing Lambda and want to write individual setting values. At that time, if you specify it directly in the source code, it may be difficult to correct it, or the value may be reused in other functions.

Then, why not prepare an ini file for setting and read it? So how to use the ini file.

The target language is Python.

Module to use

ConfigParser

ini file

Since we are using an ini file, we will upload a locally zipped version. The ini file is placed in the directory where the Python script file "lambda_function.py" is placed. This time, save it as "config.ini".

config.ini


[dynamodb]
table_name : hogehoge_table
[sns]
topic_arn : arn:aws:sns:ap-northeast-1:xxxxxxxxxxxx:xxxxxx

I will describe it like this.

how to use

After preparing the ini file, use it in the script as follows.

lambda_function.py


# -*- coding: utf-8 -*-

from __future__ import print_function
import ConfigParser
import boto3

#Read config file
ini = ConfigParser.SafeConfigParser()
ini.read("./config.ini")

# DynamoDB
dynamodb = boto3.resource('dynamodb')
dynamoTable = ini.get("dynamodb", "table_name")
dynamo = dynamodb.Table(dynamoTable)

# sns
snsTopic = ini.get("sns", "topic_arn")

def lambda_handler(event, context):
	<<Actual processing part>>

Like ini.get ("sns", "topic_arn"), specify the mass written in [xxx] as the first argument. Next, specify the element you want to take in the mass as the second argument.


I write AWS key information in a script and publish it to the public ... I often hear such stories, but I think it would be good to separate such information into ini files.

Recommended Posts

[AWS] Using ini files with Lambda [Python]
Summary if using AWS Lambda (Python)
Using Lambda with AWS Amplify with Go
Notify HipChat with AWS Lambda (Python)
Create API with Python, lambda, API Gateway quickly using AWS SAM
Connect to s3 with AWS Lambda Python
Python + Selenium + Headless Chromium with aws lambda
AWS CDK with Python
Serverless scraping using selenium with [AWS Lambda] -Part 1-
LINE BOT with Python + AWS Lambda + API Gateway
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
Upload files to Google Drive with Lambda (Python)
Dynamic HTML pages made with AWS Lambda and Python
Operate TwitterBot with Lambda, Python
[S3] CRUD with S3 using Python [Python]
[Python] Scraping in AWS Lambda
Using Quaternion with Python ~ numpy-quaternion ~
[Python] Using OpenCV with Python (Basic)
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python (3)
Check types_map when using mimetypes on AWS Lambda (Python)
Sorting image files with Python
Deploy Python3 function with Serverless Framework on AWS Lambda
Integrate PDF files with Python
AWS Lambda with PyTorch [Lambda import]
Reading .txt files with Python
Create a Layer for AWS Lambda Python with Docker
I want to AWS Lambda with Python on Mac!
Web scraping using AWS lambda
Make ordinary tweets fleet-like with AWS Lambda and Python
Using OpenCV with Python @Mac
Send using Python with Gmail
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Addictive points when downloading files using boto on AWS Lambda
Site monitoring and alert notification with AWS Lambda + Python + Slack
Complement python with emacs using company-jedi
Harmonic mean with Python Harmonic mean (using SciPy)
[Python] Loading csv files using pandas
[Python] Using OpenCV with Python (Image Filtering)
Recursively unzip zip files with python
Using Rstan from Python with PypeR
[AWS] Create API with API Gateway + Lambda
Manipulating EAGLE .brd files with Python
[Python] Using OpenCV with Python (Image transformation)
Text extraction with AWS Textract (Python3.6)
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
Write AWS Lambda function in Python
[Python] POST wav files with requests [POST]
Decrypt files encrypted with OpenSSL with Python 3
Run Python on Schedule on AWS Lambda
Tweet WakaTime Summary using AWS Lambda
Handle Excel CSV files with Python
Read files in parallel with Python
Notes on using rstrip with python.
Use PostgreSQL with Lambda (Python + psycopg2)
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
Manipulating kintone data with Python & C Data ODBC Driver from AWS Lambda