-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
70 lines (61 loc) · 2.35 KB
/
tasks.py
File metadata and controls
70 lines (61 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from invoke import task
import subprocess
import os
import shutil
import boto3
FUNCTION_NAME = 'BitcoinProgress'
FUNCTION_ARN = 'arn:aws:lambda:us-east-1:901712715767:function:updateBitcoinProgressTwitterBot' # Replace with your function ARN
ROLE_ARN = os.getenv('aws_lambda_role_arn')
HANDLER = 'src.lambda_function.run'
RUNTIME = 'python3.8'
ZIP_FILE = 'bitcoin_lambda.zip'
@task
def test(ctx):
"""Run tests using pytest"""
subprocess.run(['python', '-m', 'unittest', 'discover', '-s', 'tests'], check=True)
@task
def build(ctx):
"""Build the deployment package"""
# Create a directory for the deployment package
if not os.path.exists('build'):
os.makedirs('build')
# Copy the source files to the build directory
shutil.copytree('src', 'build/src', dirs_exist_ok=True)
# Install dependencies in the build directory
subprocess.check_call(['pip', 'install', '-r', 'requirements.txt', '-t', 'build'])
# Zip the contents of the build directory
shutil.make_archive('bitcoin_lambda', 'zip', 'build')
print("Deployment package created successfully.")
@task
def deploy(ctx):
"""Deploy the package to AWS Lambda"""
client = boto3.client('lambda', region_name="")
with open(ZIP_FILE, 'rb') as f:
zipped_code = f.read()
try:
response = client.update_function_code(
FunctionName="",
ZipFile=zipped_code,
Publish=True
)
print(f"Function {FUNCTION_NAME} updated successfully.")
except client.exceptions.ResourceNotFoundException:
# Function does not exist, create it
response = client.create_function(
FunctionName=FUNCTION_NAME,
Runtime=RUNTIME,
Role=ROLE_ARN,
Handler=HANDLER,
Code=dict(ZipFile=zipped_code),
Timeout=300, # Maximum allowable timeout
Environment={
'Variables': {
'twitter_access_token': os.getenv('twitter_access_token'),
'twitter_access_token_secret': os.getenv('twitter_access_token_secret'),
'twitter_consumer_key': os.getenv('twitter_consumer_key'),
'twitter_consumer_secret': os.getenv('twitter_consumer_secret')
}
},
Publish=True
)
print(f"Function {FUNCTION_NAME} created successfully.")