注意:
- 此教學課程需要存取 Oracle Cloud。若要註冊免費帳戶,請參閱開始使用 Oracle Cloud Infrastructure Free Tier 。
- 它會使用 Oracle Cloud Infrastructure 證明資料、租用戶及區間的範例值。完成實驗室時,請將這些值替代為您雲端環境特定的值。
使用 OCI 函數將 Oracle Cloud Infrastructure 日誌推送到 Amazon S3
簡介
Oracle Cloud Infrastructure (OCI) 日誌記錄服務可統一檢視 OCI 服務與資源產生的所有日誌,以及使用者建立的自訂日誌。
OCI Functions 是一款無伺服器運算平台,可讓開發者在雲端建置和部署輕量型、事件驅動的應用程式。
Amazon S3 是由 Amazon 提供的一種可高度擴展且可靠的雲端儲存服務,可讓使用者儲存及擷取來自全球任何位置的任何資料。
高層次處理作業包括使用服務連線器從 OCI 日誌記錄服務收集日誌,然後傳送至目標函數,再將資料推送至 Amazon S3。
目標
使用 OCI 函數將 OCI 日誌推送至 Amazon S3 的逐步操作指南。
必要條件
-
Amazon Web Services (AWS) 中的使用者必須具備建立儲存設定所需的原則,而且能夠讀取和寫入儲存設定中的物件。如需儲存桶原則的詳細資訊,請依照此頁面進行。
-
OCI 中的使用者必須具備函數、服務連線器中心以及日誌記錄服務所需的原則,才能管理資源。如需所有服務的政策參考資訊,請參閱這個網頁。
工作 1:設定 AWS 設定值
-
建立 Amazon S3 儲存桶。
-
為 IAM 底下的使用者建立 AWS 存取金鑰和秘密金鑰。
-
瀏覽至 IAM 、使用者、安全證明資料、建立存取金鑰、第三方服務。
-
將產生的 AWS 存取金鑰和秘密金鑰複製到記事本檔案。
-
作業 2:設定 Oracle Cloud Infrastructure 設定值
將 AWS 存取金鑰和秘密金鑰儲存在 OCI 保存庫
-
瀏覽至身分識別與安全、保存庫。如果未建立,請建立 Vault ,MEK。如需詳細資訊,請遵循此 doc 。
-
在保存庫中建立 2 個加密密碼,一個用於儲存 AWS 存取金鑰和 AWS 加密密碼金鑰的其他加密密碼。以下影像可供參考。
建立動態群組
-
瀏覽至識別與安全、識別、動態群組。
-
使用下方比對規則,為區間中的所有函數建立動態群組。
ALL {resource.type = 'fnfunc', resource.compartment.id = '<your_compartment_id>'}
建立動態群組的原則
-
瀏覽至身分識別與安全性、身分識別、政策。
-
建立以上動態群組的原則,讓所有函數存取個別區間的 secrets-family。該策略也可以縮小為單一 Vault。
allow dynamic-group <dynamic_group_name> to manage secret-family in compartment <compartment_name>
使用 Python 建立 OCI 函數或任何支援的語言,將 OCI 日誌傳送至 Amazon S3
-
在 Oracle Cloud 主控台功能表中,瀏覽至開發人員服務,然後選取函數。
-
選取現有的應用程式,或按一下建立應用程式。在應用程式內建立函數。
-
本教學課程將引導您使用 OCI Cloud Shell 建立此函數。
注意:建議使用 Cloud Shell 選項,因為不需要任何設定先決條件。如果您是 OCI 函數的新成員,請依照 Cloud Shell 快速入門中的 A、B 和 C 小節進行。
-
建議您先建立範例 Python 函數。下列指令會產生包含三個檔案的資料夾
ociToaws
:func.py
、func.yaml
及requirements.txt
。fn init --runtime python pushlogs
-
使用以下程式碼變更
func.py
。將程式碼中的 secret_key_id 、access_key_id 取代為先前從 OCI 保存庫建立的個別加密密碼。import io import json import logging import boto3 import oci import base64 import os from fdk import response # The below method receives the list of log entries from OCI as input in the form of bytestream and is defined in func.yaml def handler(ctx, data: io.BytesIO = None): funDataStr = data.read().decode('utf-8') # Convert the log data to json funData = json.loads(funDataStr) # The Secret Retrieval API is used here to retrieve AWS access keys and secret key from vault. These keys are required to connect to Amazon S3. # Replace secret_key_id, access_key_id with respective secret ocids. secret_key_id = "<vault_secret_ocid>" access_key_id = "<vault_secret_ocid>" signer = oci.auth.signers.get_resource_principals_signer() secret_client = oci.secrets.SecretsClient({},signer=signer) def read_secret_value(secret_client, secret_id): response = secret_client.get_secret_bundle(secret_id) base64_Secret_content = response.data.secret_bundle_content.content base64_secret_bytes = base64_Secret_content.encode('ascii') base64_message_bytes = base64.b64decode(base64_secret_bytes) secret_content = base64_message_bytes.decode('ascii') return secret_content awsaccesskey = read_secret_value(secret_client, access_key_id) awssecretkey = read_secret_value(secret_client, secret_key_id) for i in range(0,len(funData)): filename = funData[i]['time'] logging.getLogger().info(filename) # Send the log data to a temporary json file. /tmp is the supported writable directory for OCI Functions with open('/tmp/'+filename+".json", 'w', encoding='utf-8') as f: json.dump(funData[i], f, ensure_ascii=False, indent=4) # Send the log file to Amazon S3 target bucket session = boto3.Session(aws_access_key_id= awsaccesskey, aws_secret_access_key= awssecretkey) s3 = session.resource('s3') s3.meta.client.upload_file(Filename='/tmp/'+filename+'.json', Bucket='lasya-bucket', Key=filename+'.json') os.remove('/tmp/'+filename+'.json')
-
使用以下程式碼更新
func.yaml
。schema_version: 20180708 name: pushlogs version: 0.0.1 runtime: python build_image: fnproject/python:3.9-dev run_image: fnproject/python:3.9 entrypoint: /python/bin/fdk /function/func.py handler memory: 256
-
使用以下程式碼變更
requirements.txt
。fdk>=0.1.56 boto3 oci
-
使用下列指令部署函數。
fn -v deploy --app ociToaws
建立「服務連線器」以將 OCI 日誌從日誌記錄傳送至函數
-
在「主控台」功能表中,依序選取「分析與 AI、訊息傳遞」和「服務連線器中心」。
-
選取要建立服務連線器的區間。
-
按一下建立服務連線器並提供個別的值。在「設定服務連線器」底下,選取「串流處理」作為來源,然後選取「日誌記錄分析」作為目標。
-
在「設定來源」下,選取要推送至 Functions 的日誌
-
在「設定目的地」下,選取我們在步驟 4 中建立的「函數應用程式」。
-
建立畫面上顯示的預設原則,然後按一下建立。
作業 3:確認日誌是否已推送至 Amazon S3
-
瀏覽至先前建立的 Amazon S3 儲存桶,確認是否有 OCI 日誌可用。
接下來的步驟
本教學課程示範如何使用服務連線器中心和函數,將 Oracle Cloud Infrastructure 日誌推送至 Amazon S3。OCI 日誌資料可用於多種儲存策略,以實現備援和合規性,並與 AWS 內的現有連接器進行詳細分析。
確認書
認證者 - Lasya Vadavalli (雲端工程師 -IaaS) 的 Vishak Chittuvalapil (資深雲端工程師)
其他學習資源
探索 docs.oracle.com/learn 的其他實驗室,或者存取更多 Oracle Learning YouTube 頻道上的免費學習內容。此外,請瀏覽 education.oracle.com/learning-explorer 以成為 Oracle Learning 檔案總管。
如需產品文件,請造訪 Oracle Help Center 。
Push Oracle Cloud Infrastructure Logs to Amazon S3 with OCI Functions
F82371-01
June 2023
Copyright © 2023, Oracle and/or its affiliates.