<jupyter_start><jupyter_text>CF 를 위한 코드 ZIP 파일 패키징  1. 환경 설정 [중요] bucket 이름은 반드시 바꾸어 주세요. !!!<jupyter_code># bucket = 'kr-lex-workshop'
# region = 'ap-northeast-2'
# project_prefix = 'Lex-KR_BankingBot'
# aws_profile = 'default'<jupyter_output><empty_output><jupyter_text>개인 곤수 계정<jupyter_code>bucket = 'gonsoomoon-priviate-share'
region = 'ap-northeast-2'
project_prefix = 'Lex-KR_BankingBot'
aws_profile = 'private-gs'
Biz_DDB_Code_Folder = 'Artifact/Lambda-BizLogic-DynamoDB-Code'
Biz_Logic_Code_Folder = 'Artifact/Lambda-BizLogic-BankingService-Code'
Lex_Artifact_Folder = 'Artifact/Lambda-LexArtifact-Export-Artifact'<jupyter_output><empty_output><jupyter_text>2. DDB 레코드 입력 람다 코드 준비<jupyter_code>DDBInsertFolder = "Deploy-Package/Lambda-BizLogic-DynamoDB"
DDBInsertCodeZip = "DDBInsert.zip"<jupyter_output><empty_output><jupyter_text>(1) 코드 파일, 의존성 패키지를 압축한 Zip 파일 생성  Shell 명령어 설명- DDBInsertCodeZip 파일이 존재하면 삭제- 의존 패키지가 저장된 package 폴더가 있으면 삭제- 의존성 패키지 requests 설치- 의존성 패키지를 DDBInsertCodeZip 파일에 추가- DDBInsertCodeZip 파일에 lambda_function.py cfnresponse.py BankingBotData.json 파일 추가<jupyter_code>%%sh -s {DDBInsertFolder} {DDBInsertCodeZip} {Biz_DDB_Code_Folder}
DynamoTableInsertFolder=$1
DBInsertCodeZip=$2
Biz_DDB_Code_Folder=$3

echo $DynamoTableInsertFolder
echo $DBInsertCodeZip

# 코드 있는 폴더로 이동
cd $DynamoTableInsertFolder

# 코드를 압축한 zip 파일 있으면 삭제
if test -f $DBInsertCodeZip; then
    rm $DBInsertCodeZip
    echo "Existing Code Zip file is deleted"
fi

# package 폴더 있으면 삭제
if [ -d "package" ]; then
    rm -rf package
    echo "existing package folder is deleted" 
fi    

# requests 패키지 다운로드
pip install --target ./package requests

cd package
# 패키지 폴더 내용 압축
zip -r ../$DBInsertCodeZip .
cd ..

echo 'Current Director: '
echo $PWD
# 코드 파일 및 데이터 파일을 압축 파일에 추가
zip -g $DBInsertCodeZip ../../$Biz_DDB_Code_Folder/*.py ../../$Biz_DDB_Code_Folder/*.json<jupyter_output>Deploy-Package/Lambda-BizLogic-DynamoDB
DDBInsert.zip
Existing Code Zip file is deleted
existing package folder is deleted
Collecting requests
  Using cached requests-2.27.1-py2.py3-none-any.whl (63 kB)
Collecting idna<4,>=2.5
  Using cached idna-3.3-py3-none-any.whl (61 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached urllib3-1.26.8-py2.py3-none-any.whl (138 kB)
Collecting charset-normalizer~=2.0.0
  Using cached charset_normalizer-2.0.10-py3-none-any.whl (39 kB)
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2021.10.8 charset-normalizer-2.0.10 idna-3.3 requests-2.27.1 urllib3-1.26.8
  adding: requests-2.27.1.dist-info/ (stored 0%)
  adding: requests-2.27.1.dist-info/INSTALLER (stored 0%)
  adding: requests-2.27.1.dist-info/top_level.txt (stored 0%)
  adding: requests-2.27.1.dist-info/RECORD (deflated 55%)
  adding: request[...]<jupyter_text>(2) Zip 파일을 S3에 업로드<jupyter_code>import os

prefix = 'Lambda-BizLogic-DynamoDB'
file_name = DDBInsertCodeZip

s3_file_path = 's3://' + os.path.join(bucket, project_prefix, prefix, file_name)
local_file_path = os.path.join(DDBInsertFolder, file_name)

print("s3_file_path: ", s3_file_path)
print("local_file_path: ", local_file_path)



! aws s3 cp {local_file_path} {s3_file_path} --profile {aws_profile}
! aws s3 ls {bucket} --recursive --profile {aws_profile}<jupyter_output>upload: Deploy-Package/Lambda-BizLogic-DynamoDB/DDBInsert.zip to s3://gonsoomoon-priviate-share/Lex-KR_BankingBot/Lambda-BizLogic-DynamoDB/DDBInsert.zip
2020-10-14 06:11:10   86513735 201014-AWS AIML Healthcare-gonsoomoon.pdf
2020-10-21 10:00:07  159622216 201021-AWS AIML Healthcare-gonsoomoon.pptx
2021-01-07 04:19:37          0 210107-Reinvent-BespinGlobal/
2021-01-07 04:25:10 1086597285 210107-Reinvent-BespinGlobal/2020-Reinvent-BaspinGlobal.zip
2020-03-18 03:24:58       2024 DaekeunPersonalizeDemo.yaml
2020-07-13 08:19:35       1587 GonsooPersonalizeForecastDemo.yaml
2021-12-08 07:15:05       8616 Lex-KR_BankingBot/CF-YAML-File/CF_KRBankingBot.yaml
2021-12-08 07:14:50       5535 Lex-KR_BankingBot/Lambda-BizLogic-BankingService/BankingServiceFunction.zip
2022-01-27 14:02:33     816256 Lex-KR_BankingBot/Lambda-BizLogic-DynamoDB/DDBInsert.zip
2021-12-08 07:15:03     824436 Lex-KR_BankingBot/Lambda-LexArtifact-Import/LexImport.zip
2020-07-13 08:20:41         36 public_test<jupyter_text>3. 비즈 로직 Banking Service 람다 코드 준비 터미널에서 아래 명령어 실행```cd Lambda-BankingService/npm install requestsrm BankingServiceFunction.zip zip -r BankingServiceFunction.zip .aws lambda update-function-code --function-name BankingServiceFunction --zip-file fileb://BankingServiceFunction.zip```<jupyter_code>BizLambdaFolder = "Deploy-Package/Lambda-BizLogic-BankingService"
BizLambdaCodeZip = "BankingServiceFunction.zip"
%%sh -s {BizLambdaFolder} {BizLambdaCodeZip} {Biz_Logic_Code_Folder}
BizLambdaFolder=$1
BizLambdaCodeZip=$2
Biz_Logic_Code_Folder=$3

echo $BizLambdaFolder
echo $BizLambdaCodeZip
echo $Biz_Logic_Code_Folder

# 코드 있는 폴더로 이동
cd $BizLambdaFolder

# 코드를 압축한 zip 파일 있으면 삭제
if test -f $BizLambdaCodeZip; then
    rm $BizLambdaCodeZip
    echo "Existing Code Zip file is deleted"
fi

echo 'Current Directory: '
echo $PWD 

# 코드 파일 및 데이터 파일을 압축 파일에 추가
zip -g $BizLambdaCodeZip ../../$Biz_Logic_Code_Folder/*.js



import os

prefix = 'Lambda-BizLogic-BankingService'
file_name = "BankingServiceFunction.zip"

s3_file_path = 's3://' + os.path.join(bucket, project_prefix, prefix, file_name)
local_file_path = os.path.join(BizLambdaFolder, file_name)

print("s3_file_path: ", s3_file_path)
print("local_file_path: ", local_file_path)



! aws s3 cp {local_file_path} {s3_file_path} --profile {aws_profile}
! aws s3 ls {bucket} --recursive --profile {aws_profile}<jupyter_output>upload: Deploy-Package/Lambda-BizLogic-BankingService/BankingServiceFunction.zip to s3://gonsoomoon-priviate-share/Lex-KR_BankingBot/Lambda-BizLogic-BankingService/BankingServiceFunction.zip
2020-10-14 06:11:10   86513735 201014-AWS AIML Healthcare-gonsoomoon.pdf
2020-10-21 10:00:07  159622216 201021-AWS AIML Healthcare-gonsoomoon.pptx
2021-01-07 04:19:37          0 210107-Reinvent-BespinGlobal/
2021-01-07 04:25:10 1086597285 210107-Reinvent-BespinGlobal/2020-Reinvent-BaspinGlobal.zip
2020-03-18 03:24:58       2024 DaekeunPersonalizeDemo.yaml
2020-07-13 08:19:35       1587 GonsooPersonalizeForecastDemo.yaml
2021-12-08 07:15:05       8616 Lex-KR_BankingBot/CF-YAML-File/CF_KRBankingBot.yaml
2022-01-27 14:08:54       6147 Lex-KR_BankingBot/Lambda-BizLogic-BankingService/BankingServiceFunction.zip
2022-01-27 14:02:33     816256 Lex-KR_BankingBot/Lambda-BizLogic-DynamoDB/DDBInsert.zip
2021-12-08 07:15:03     824436 Lex-KR_BankingBot/Lambda-LexArtifact-Import/LexImport.zip
2020-07-13 08:20:4[...]<jupyter_text>4. 렉스 봇 아티펙트 설치 람다 코드 준비 폴더 구조- request 라이브러리- bot_details.json    ```{  "name": "CardServices",  "bot_definition": "CardServicesBot-DRAFT-AIGCF4Y0R2-LexJson.zip"}        ```- Bot 익스포트 zip 파일 (에: "CardServicesBot-DRAFT-AIGCF4Y0R2-LexJson.zip")- cfnresponse.py- lambda_function.py  (1) Lex Import ZIP 생성<jupyter_code>LexImportFolder = "Deploy-Package/Lambda-LexArtifact-Import"
LexImportCodeZip = "LexImport.zip"
%%sh -s {LexImportFolder} {LexImportCodeZip} {Lex_Artifact_Folder}
LexImportFolder=$1
LexImportCodeZip=$2
Lex_Artifact_Folder=$3

echo $LexImportFolder
echo $LexImportCodeZip
echo $Lex_Artifact_Folder


# 코드 있는 폴더로 이동
cd $LexImportFolder

# 코드를 압축한 zip 파일 있으면 삭제
if test -f $LexImportCodeZip; then
    rm $LexImportCodeZip
    echo "Existing Code Zip file is deleted"
fi

# package 폴더 있으면 삭제
if [ -d "package" ]; then
    rm -rf package
    echo "existing package folder is deleted" 
fi    

# requests 패키지 다운로드
pip install --target ./package requests

cd package
# 패키지 폴더 내용 압축
zip -r ../$LexImportCodeZip .
cd ..

echo 'Current Directory: '
echo $PWD 


# 코드 파일 및 데이터 파일을 압축 파일에 추가
zip -g $LexImportCodeZip ../../$Lex_Artifact_Folder/*.py  ../../$Lex_Artifact_Folder/*.json ../../$Lex_Artifact_Folder/*.zip



import os

prefix = 'Lambda-LexArtifact-Import'
file_name = LexImportCodeZip

s3_file_path = 's3://' + os.path.join(bucket, project_prefix, prefix, file_name)
local_file_path = os.path.join(LexImportFolder, file_name)

print("s3_file_path: ", s3_file_path)
print("local_file_path: ", local_file_path)


! aws s3 cp {local_file_path} {s3_file_path} --profile {aws_profile}
! aws s3 ls {bucket} --recursive --profile {aws_profile}<jupyter_output>upload: Deploy-Package/Lambda-LexArtifact-Import/LexImport.zip to s3://gonsoomoon-priviate-share/Lex-KR_BankingBot/Lambda-LexArtifact-Import/LexImport.zip
2020-10-14 06:11:10   86513735 201014-AWS AIML Healthcare-gonsoomoon.pdf
2020-10-21 10:00:07  159622216 201021-AWS AIML Healthcare-gonsoomoon.pptx
2021-01-07 04:19:37          0 210107-Reinvent-BespinGlobal/
2021-01-07 04:25:10 1086597285 210107-Reinvent-BespinGlobal/2020-Reinvent-BaspinGlobal.zip
2020-03-18 03:24:58       2024 DaekeunPersonalizeDemo.yaml
2020-07-13 08:19:35       1587 GonsooPersonalizeForecastDemo.yaml
2021-12-08 07:15:05       8616 Lex-KR_BankingBot/CF-YAML-File/CF_KRBankingBot.yaml
2022-01-27 14:08:54       6147 Lex-KR_BankingBot/Lambda-BizLogic-BankingService/BankingServiceFunction.zip
2022-01-27 14:02:33     816256 Lex-KR_BankingBot/Lambda-BizLogic-DynamoDB/DDBInsert.zip
2022-01-27 14:12:11     825595 Lex-KR_BankingBot/Lambda-LexArtifact-Import/LexImport.zip
2020-07-13 08:20:41         36 public_test<jupyter_text>5. CF YAML 파일 및 준비 파일 업로드<jupyter_code>CFFolder = "Deploy-Package"
import os

prefix = "CF-YAML-File"
file_name = 'CF_KRBankingBot.yaml'

s3_file_path = 's3://' + os.path.join(bucket, project_prefix, prefix, file_name)
local_file_path = os.path.join(CFFolder, file_name)

print("s3_file_path: ", s3_file_path)
print("local_file_path: ", local_file_path)

! aws s3 cp {local_file_path} {s3_file_path} --profile {aws_profile}
! aws s3 ls {bucket} --recursive --profile {aws_profile}<jupyter_output>upload: Deploy-Package/CF_KRBankingBot.yaml to s3://gonsoomoon-priviate-share/Lex-KR_BankingBot/CF-YAML-File/CF_KRBankingBot.yaml
2020-10-14 06:11:10   86513735 201014-AWS AIML Healthcare-gonsoomoon.pdf
2020-10-21 10:00:07  159622216 201021-AWS AIML Healthcare-gonsoomoon.pptx
2021-01-07 04:19:37          0 210107-Reinvent-BespinGlobal/
2021-01-07 04:25:10 1086597285 210107-Reinvent-BespinGlobal/2020-Reinvent-BaspinGlobal.zip
2020-03-18 03:24:58       2024 DaekeunPersonalizeDemo.yaml
2020-07-13 08:19:35       1587 GonsooPersonalizeForecastDemo.yaml
2022-01-27 14:13:07       8616 Lex-KR_BankingBot/CF-YAML-File/CF_KRBankingBot.yaml
2022-01-27 14:08:54       6147 Lex-KR_BankingBot/Lambda-BizLogic-BankingService/BankingServiceFunction.zip
2022-01-27 14:02:33     816256 Lex-KR_BankingBot/Lambda-BizLogic-DynamoDB/DDBInsert.zip
2022-01-27 14:12:11     825595 Lex-KR_BankingBot/Lambda-LexArtifact-Import/LexImport.zip
2020-07-13 08:20:41         36 public_test<jupyter_text>CF S3 경로<jupyter_code>print(f"https://{bucket}.s3.{region}.amazonaws.com/{project_prefix}/{prefix}/{file_name}")
# ! aws s3 rm s3://{bucket} --recursive<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11555795925725618, 'fraction_numerical': 0.1014963043086353, 'mean_word_length': 4.827205882352941, 'pattern_counts': {'":': 2, '<': 34, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '45160', 'license': 'MIT-0', 'n_tokens_mistral': 5540, 'n_tokens_neox': 4989, 'n_words': 825, 'path': 'aiservices/lex-korean-workshop/Create-CF-Lex-KR-Workshop/1.2.Prepare-Artifact_Public_S3.ipynb', 'repo_name': 'gonsoomoon-ml/aws-ai-ml-workshop-kr', 'size': '11094'}