Setting Up AWS Environment
- Create an IAM role with necessary policies for Lambda or EC2 instances to access external APIs.
- Ensure that your AWS service (Lambda/EC2) has the necessary internet access.
Configuring OpenAI API Key
- Store your OpenAI API key securely using AWS Secrets Manager or AWS Systems Manager Parameter Store.
- Use AWS SDK to retrieve the API key securely at runtime from these services.
Authenticating API Requests
- In your AWS-hosted application, include the OpenAI API key in the HTTP headers of your requests.
import boto3
import requests
def get_openai_key():
ssm = boto3.client('ssm')
response = ssm.get_parameter(Name='OpenAI_API_Key', WithDecryption=True)
return response['Parameter']['Value']
def call_openai_api():
api_key = get_openai_key()
headers = {"Authorization": f"Bearer {api_key}"}
response = requests.get("https://api.openai.com/v1/examples", headers=headers)
return response.json()
result = call_openai_api()
print(result)