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.
ConfigParser
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.
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