Notice
Recent Posts
Recent Comments
Link
투케이2K
222. (python/파이썬) [AWS] [Lambda] 런타임 Python 3.13 - boto3 모듈 사용해 service-quotas iot 서비스 할당량 제한 확인 본문
Python
222. (python/파이썬) [AWS] [Lambda] 런타임 Python 3.13 - boto3 모듈 사용해 service-quotas iot 서비스 할당량 제한 확인
투케이2K 2026. 1. 7. 22:25728x90
[개발 환경 설정]
개발 툴 : Aws / Lambda / Runtime Python 3.13
개발 언어 : python

[소스 코드]
// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------
- 언어 : Python
- 개발 툴 : Aws / Lambda / Runtime Python 3.13
- 개발 기술 : AWS Lambda 이벤트 동작 함수
- 사전) AWS Lambda 설명 :
>> Aws Lambda 는 서버 리스 FaaS 솔루션으로, 함수의 인스턴스를 실행하여 이벤트를 처리할 수 있습니다.
>> Aws Lambda 는 이벤트에 응답하여 코드를 실행 하고 해당 코드에 필요한 컴퓨팅 리소스를 자동으로 관리합니다.
- 사전) Aws Iot Core 간략 설명 :
>> AWS IoT 는 IoT 디바이스를 다른 디바이스 및 AWS 클라우드 서비스에 연결하는 클라우드 서비스를 제공합니다.
>> 디바이스가에 연결할 수 있는 경우 AWS IoT는 AWS 가 제공하는 클라우드 서비스에 디바이스를 AWS IoT 연결할 수 있습니다.
>> AWS IoT Core 메시지 브로커는 MQTT 및 MQTT over WSS 프로토콜을 사용하여 메시지를 게시하고 구독하는 디바이스 및 클라이언트를 지원합니다.
- HTTPS 프로토콜을 사용하여 메시지를 게시하는 디바이스와 클라이언트도 지원합니다.
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[소스 코드]
// --------------------------------------------------------------------------------------
# ========================================================================
# [Aws] : [boto3 모듈] : IAM 계정 정보 사용해 Aws service-quotas 할당량 정보 조회 수행
# ========================================================================
# [참고] : API Gateway 와 연동 되어 Post 방식으로 Lambda 함수 호출 및 응답 값 반환 수행
# ========================================================================
"""
1. aws lambda python 3.13 런타임 환경 기반
2. boto3 모듈 기본 내장 AWS 사용 모듈
3. import ClientError : AWS SDK for Python 인 boto3 에서 발생할 수 있는 예외 중 하나로, AWS 서비스 호출 중 오류가 발생했을 때 사용됩니다
"""
# ========================================================================
import json
import os
import boto3
from botocore.exceptions import ClientError
from decimal import Decimal
def lambda_handler(event, context): # Lambda 호출 시 동작 되는 메인 함수
# [event , context 정보 디버깅 로그 출력]
print(f"DLOG = event : {event} / context {context}")
# [Return 반환 Json 변수 선언]
response = {
"statusCode" : 0,
"headers" : {},
"body" : ""
}
# [AWS IAM 계정 AccessKey, SecretKey 변수 선언]
iamAccessKey = "AK..A6"
iamSecretKey = "mP..5J"
iamRegion = "ap-northeast-2" # 리전
# [명시적 인증 정보로 세션 생성]
session = boto3.Session(
aws_access_key_id = iamAccessKey,
aws_secret_access_key = iamSecretKey,
region_name = iamRegion
)
# [AWS 클라이언트 생성]
aws_client = session.client('service-quotas')
try:
# [service-quotas 할당량 정보 요청 수행]
requestServiceCode = 'iot' # ✅ 할당량 정보 확인 서비스 명칭
aws_response = aws_client.list_service_quotas(ServiceCode=requestServiceCode)
print(f"DLOG = aws_response : {aws_response}")
# for 문을 수행하며 개별 데이터 확인
for quota in aws_response['Quotas']:
print(f"FOR = QuotaName : {quota['QuotaName']} / Value : {quota['Value']}")
# print(quota['QuotaName'], quota['Value'])
# [리턴 변수 삽입] : ApiGateWay 응답 반환 설정 : Lambda 통합 요청 사용
response["statusCode"] = 200
response["headers"] = {
"Content-Type": "application/json",
'Access-Control-Allow-Origin': '*', # CORS 허용 (필요 시)
}
response["body"] = json.dumps({
"data": aws_response
})
except ClientError as e: # AWS 서비스 호출 중 오류 발생 처리
error_code = e.response['Error']['Code']
error_message = e.response['Error']['Message']
# [리턴 변수 삽입]
response["statusCode"] = 400
response["headers"] = {
"Content-Type": "application/json",
'Access-Control-Allow-Origin': '*', # CORS 허용 (필요 시)
}
response["body"] = json.dumps(
{
"exception" : "ClientError",
"error_code" : error_code,
"error_message" : error_message
}
)
# [리턴 반환 수행]
return response
# ========================================================================
"""
Response:
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
"body": "{\"data\": {\"Quotas\": [{\"ServiceCode\": \"iot\", \"ServiceName\": \"AWS IoT\", \"QuotaArn\": \"arn:aws:servicequotas:ap-northeast-2:123456789012:iot/L-F570A784\", \"QuotaCode\": \"L-F570A784\", \"QuotaName\": \"Parameters per dynamic command\", \"Value\": 5.0, \"Unit\": \"None\", \"Adjustable\": true, \"GlobalQuota\": false, \"QuotaAppliedAtLevel\": \"ACCOUNT\", \"Description\": \"The maximum number of parameters supported per dynamic command in your account\"}, {\"ServiceCode\": \"iot\", \"ServiceName\": \"AWS IoT\", \"QuotaArn\": \"arn:aws:servicequotas:ap-northeast-2:123456789012:iot/L-CB4FF40C\", \"QuotaCode\": \"L-CB4FF40C\", \"QuotaName\": \"Leading Wildcard Query rate\", \"Value\": 30.0, \"Unit\": \"None\", \"Adjustable\": true, \"GlobalQuota\": false, \"QuotaAppliedAtLevel\": \"ACCOUNT\", \"Description\": \"The maximum number of Leading Wildcard Queries per second across all APIs.\"}], \"ResponseMetadata\": {\"RequestId\": \"fe6b8ca4-0182-416d-b569-9f583745914f\", \"HTTPStatusCode\": 200, \"HTTPHeaders\": {\"date\": \"Wed, 07 Jan 2026 00:39:26 GMT\", \"content-type\": \"application/x-amz-json-1.1\", \"content-length\": \"776\", \"connection\": \"keep-alive\", \"x-amzn-requestid\": \"fe6b8ca4-0182-416d-b569-9f583745914f\"}, \"RetryAttempts\": 0}}}"
}
"""
# ========================================================================
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------
[Amazon API Gateway] Aws API Gateway 게이트웨이 설명 정리 - 중개 서버
https://blog.naver.com/kkh0977/223827753479
[Amazon API Gateway] Aws API Gateway 게이트웨이 API 엔드포인트 유형 정리
https://blog.naver.com/kkh0977/223911565693
[Aws Lambda] Aws 사이트에서 생성 된 Lambda 람다 검증 함수 리스트 및 내용 소스 코드 확인 방법
https://blog.naver.com/kkh0977/223765198383
[AWS] Lambda 람다 함수 수행 errorType Sandbox.Timedout 에러 발생
https://blog.naver.com/kkh0977/223962778768
// --------------------------------------------------------------------------------------
728x90
반응형
'Python' 카테고리의 다른 글
Comments
