How to use lambda_layer_arn method in localstack

Best Python code snippet using localstack_python

core_aws_lambda.py

Source:core_aws_lambda.py Github

copy

Full Screen

1from urllib.parse import urlparse2import boto33from botocore.exceptions import ClientError45from awsdest.utils.core_aws import *67def _isthere__s3_bucketfolder_notification_config(awsconfig, s3folder):8 parsed = urlparse(s3folder, allow_fragments=False)9 bucketin = parsed.netloc10 s3folder_prefix = parsed.path.lstrip('/')11 try:12 s3_client = boto3.client('s3', aws_access_key_id=awsconfig['aws_access_key_id'],13 aws_secret_access_key=awsconfig['aws_secret_access_key'])14 response = s3_client.get_bucket_notification_configuration(Bucket=bucketin)1516 if 'LambdaFunctionConfigurations' in response or \17 'TopicConfigurations' in response or \18 'QueueConfigurations' in response :19 # print("This bucket does event config set"20 return True2122 return False2324 # following will never be executed by design. Since we want to aboid creating event notifications if there is some on bucket already.25 # our process wipes out previos configurations.26 lambdaFuncConfigs_list = response['LambdaFunctionConfigurations']27 for lc in lambdaFuncConfigs_list:28 lc1 = lc['Filter']['Key']['FilterRules']29 for lc2 in lc1:30 if lc2['Name'] == "Prefix" and str.rstrip(lc2['Value'], "/") == str.rstrip(s3folder_prefix,"/"): # if folder prefix matches then there is a notification on that folder prefix31 print("This s3bucket:/folder has lambda functon configuration already:", lc)32 return True3334 except ClientError as e:35 print("Error getting s3_event_configuration " % e)36 return Null373839def validate_lambda_folders(awsconfig, s3folderin_lambda, s3folderout_lambda):40 if number_of_files_s3folder(awsconfig, s3folderin_lambda) < 0:41 print("S3 input folder non existent.")42 return False4344 if number_of_files_s3folder(awsconfig, s3folderout_lambda) < 0:45 print("S3 output folder non existent.")46 return False4748 # check if input and output folder as same. We do not want events/triggers that result in an infinite loop of executions49 if str.rstrip(s3folderin_lambda, "/") == str.rstrip(s3folderout_lambda, "/"):50 print(" cannot have input and output folders same to prevent infinite lambda loop triggers")51 return False5253 # check if there are events/lambda defined on this bucket and folder specifically54 # we do not want conflicts. So being extra careful.55 if _isthere__s3_bucketfolder_notification_config(awsconfig, s3folderin_lambda):56 print("Event configuration exists on this bucket. Do not want to create additional configuration:",57 s3folderin_lambda)58 return False5960 if _isthere__s3_bucketfolder_notification_config(awsconfig, s3folderout_lambda):61 print("Event configuration exists on this folder. Do not want to create additional configuration:",62 s3folderout_lambda)63 return False6465 return True666768def create_lambda_layer(awsconfig, lambdaconfig):69 l = open('lambda_layers/lambda_layers_k8s_requests_etc_packages_1.zip', 'rb')70 layer_zipfile = l.read()7172 try:73 lambda_client = boto3.client('lambda', aws_access_key_id=awsconfig['aws_access_key_id'],74 aws_secret_access_key=awsconfig['aws_secret_access_key'])75 response = lambda_client.publish_layer_version(LayerName='lambda_scoring_layer',76 Content={'ZipFile': layer_zipfile77 },78 )79 lambda_layer_arn = response['LayerArn']80 lambda_layer_version = response['Version']8182 #layer_arn_ver = 'arn:aws:lambda:us-east-1:617292774228:layer:lambda_scoring_layer:1'83 #response = lambda_client.add_layer_version_permission(84 # LayerName='lambda_scoring_layer',85 # VersionNumber='$LATEST',86 # StatementId='1',87 # Action='lambda:GetLayerVersion',88 # Principal='*'89 #)9091 return (lambda_layer_arn, lambda_layer_version)9293 except ClientError as e:94 print("Error creating lambda layer " % e)95 return Null969798def _create_lambda_function(awsconfig, lambdaconfig, k8sconfig, model_imagename, s3folderin_lambda, s3folderout_lambda):99 aws_eks_cluster_name = awsconfig['aws_eks_cluster_name']100 ingress_controller_url = awsconfig['ingress_controller_url']101 aws_region = awsconfig['aws_region']102 k8s_namespace = k8sconfig['k8s_namespace']103104 lambda_execution_role = lambdaconfig['lambda_execution_arn_role']105 lambda_function_name = lambdaconfig['lambda_function_name']106 lambda_layer_arn = lambdaconfig['lambda_layer_arn']107 lambda_layer_version = lambdaconfig['lambda_layer_version']108109 f = open('lambda_function_dependencies/lambdafunc_scoring_dependencies.zip', 'rb')110 function_zipfile = f.read()111112 try:113 lambda_client = boto3.client('lambda', aws_access_key_id=awsconfig['aws_access_key_id'],114 aws_secret_access_key=awsconfig['aws_secret_access_key'])115116 lambda_layer_arn_version = lambda_layer_arn + ":" + lambda_layer_version117118 response = lambda_client.create_function(119 FunctionName=lambda_function_name,120 Runtime='python3.7',121 Role=lambda_execution_role,122 Handler='lambdafunc_scoring.scoring_lambda_handler',123 Code={124 'ZipFile': function_zipfile,125 },126 Timeout=899,127 MemorySize=128,128 Environment={129 'Variables': {130 'fallback_modelname': model_imagename,131 'fallback_clustername': aws_eks_cluster_name,132 'fallback_ingress_url' : ingress_controller_url,133 's3_outputfolder': s3folderout_lambda,134 'aws_region': aws_region,135 'k8s_namespace' : k8s_namespace136 }137 },138 Layers=[lambda_layer_arn_version]139 )140 functionArn = response['FunctionArn']141142 response = lambda_client.add_permission(143 FunctionName=functionArn,144 StatementId='1',145 Action='lambda:InvokeFunction',146 Principal='s3.amazonaws.com'147 )148 return functionArn149150 except ClientError as e:151 print("Error creating lambda function " % e)152 return Null153154155def _create_s3bucket_notificationfunc(awsconfig, lambdaconfig, s3folderin_lambda, functionArn):156 parsed = urlparse(s3folderin_lambda, allow_fragments=False)157 bucketin = parsed.netloc158 s3prefix = parsed.path.lstrip('/')159160 lambda_function_name = lambdaconfig['lambda_function_name']161162 try:163 s3_client = boto3.client('s3', aws_access_key_id=awsconfig['aws_access_key_id'],164 aws_secret_access_key=awsconfig['aws_secret_access_key'])165 response = s3_client.put_bucket_notification_configuration(166 Bucket=bucketin,167 NotificationConfiguration={168 'LambdaFunctionConfigurations': [169 {170 'Id': lambda_function_name,171 'LambdaFunctionArn': functionArn,172 'Events': [173 's3:ObjectCreated:*'174 ],175 'Filter': {176 'Key': {177 'FilterRules': [178 {179 'Name': 'prefix', 'Value': s3prefix180 },181 {182 'Name': 'suffix', 'Value': '.csv'183 }184 ]185 }186 }187 }188 ]189 }190 )191 print("Event Notification created on S3 Input bucket that invokes above Lambda Function", bucketin)192 except ClientError as e:193 print("Error creating event notification configuration on s3 bucket " % e)194 return Null195 # except Exception as e:196 # print(str(e))197 # raise e198199200def create_lambda_setup(awsconfig,lambdaconfig, k8sconfig, model_imagename, s3folderin_lambda, s3folderout_lambda):201 functionArn = _create_lambda_function(awsconfig, lambdaconfig, k8sconfig, model_imagename, s3folderin_lambda, s3folderout_lambda)202 print("Lambda Function created functionArn:", functionArn)203 _create_s3bucket_notificationfunc(awsconfig, lambdaconfig, s3folderin_lambda, functionArn)204205206######207208def _delete_s3bucket_notificationfunc(awsconfig, s3folderin_lambda):209 parsed = urlparse(s3folderin_lambda, allow_fragments=False)210 bucketin = parsed.netloc211 try:212 # delete by saving an empty notification configuration.213 s3_client = boto3.client('s3', aws_access_key_id=awsconfig['aws_access_key_id'],214 aws_secret_access_key=awsconfig['aws_secret_access_key'])215 response = s3_client.put_bucket_notification_configuration(216 Bucket=bucketin,217 NotificationConfiguration={218 }219 )220 print("Deleted ALL Event notification configurations on bucket ", )221 except ClientError as e:222 print("Error creating event notification configuration on s3 bucket " % e)223 return Null224 # except Exception as e:225 # print(str(e))226 # raise e227228229def _delete_lambda_function(awsconfig, lambdaconfig):230 lambda_function_name = lambdaconfig['lambda_function_name']231232 try:233 lambda_client = boto3.client('lambda', aws_access_key_id=awsconfig['aws_access_key_id'],234 aws_secret_access_key=awsconfig['aws_secret_access_key'])235 response = lambda_client.delete_function(FunctionName=lambda_function_name)236237 # do not delete layer here. see why @ https://stackoverflow.com/questions/60824745/aws-delete-lambda-layer-still-retains-layer-version-history/61103244#61103244238239 return True240241 except ClientError as e:242 print("Error creating lambda function or layer " % e)243 return Null244245246def delete_lambda_setup(awsconfig, lambdaconfig, s3folderin_lambda):247 _delete_s3bucket_notificationfunc(awsconfig,s3folderin_lambda) # deletes all notifcations. Bad. Restricting to folder prefix is not provided by AWS SDK. ...

Full Screen

Full Screen

arns.py

Source:arns.py Github

copy

Full Screen

1"""2Functions that will ensure that the ARN is returned if a string is passed.3If it is an object in troposphere, it will return GetAtt(obj, 'Arn') or Ref() depending on4what the object supports for return.5If it is a string, it must either comply to the last part of a ARN or be a full ARN6and match the ARN pattern7"""8import re9from troposphere import (10 AWS_REGION,11 AWS_ACCOUNT_ID12)13from troposphere import (14 ImportValue,15 Parameter,16 GetAtt,17 Sub,18 Ref19)20from troposphere.iam import (21 Role22)23from troposphere.s3 import Bucket24from troposphere.awslambda import Function25from troposphere.kms import (26 Key, Alias27)28from ozone.filters.regexes import (29 S3_ARN_PREFIX, S3_NAME, S3_ARN,30 IAM_ROLE_NAME, IAM_ROLE_ARN,31 LAMBDA_NAME, LAMBDA_ARN,32 LAMBDA_LAYER_VERSION, LAMBDA_LAYER_ARN,33 KMS_KEY_ARN, KMS_KEY_ID,34 KMS_ALIAS, KMS_ALIAS_ARN35)36def s3_bucket(bucket, any_object=False):37 """38 Args:39 bucket: represents the bucket object, or a function40 Returns:41 untouched if one of the functions supported42 string of the full ARN if the bucket name is given43 full ARN if full ARN is given and match S3 bucket ARN pattern44 """45 arn_pat = re.compile(S3_ARN)46 name_pat = re.compile(S3_NAME)47 if isinstance(bucket, (ImportValue, GetAtt, Sub, Ref)):48 return bucket49 elif isinstance(bucket, Parameter):50 if any_object:51 return Sub('arn:aws:s3:::{bucket}/*')52 else:53 return Sub('arn:aws:s3:::{bucket}')54 elif isinstance(bucket, Bucket):55 return GetAtt(bucket, 'Arn')56 elif isinstance(bucket, str):57 if arn_pat.match(bucket):58 return bucket59 elif name_pat.match(bucket):60 if any_object:61 return f'{S3_ARN_PREFIX}{bucket}/*'62 else:63 return f'{S3_ARN_PREFIX}{bucket}'64 else:65 raise ValueError('The S3 ARN must follow', S3_ARN)66 else:67 raise ValueError(68 'The S3 ARN must be computed with a function or follow the pattern',69 S3_ARN70 )71def iam_role(role):72 """73 Args:74 role: represents the role object, or a function75 Returns:76 untouched if one of the functions supported77 string of the full ARN if the role name is given78 full ARN if full ARN is given and match IAM role ARN pattern79 """80 arn_pattern = re.compile(IAM_ROLE_ARN)81 name_pattern = re.compile(IAM_ROLE_NAME)82 if isinstance(role, str):83 if name_pattern.match(role):84 role_arn = Sub(f'arn:aws:iam::${{AWS::AccountId}}:role/{role}')85 elif role.startswith('arn:aws:iam::') and arn_pattern.match(role):86 role_arn = role87 else:88 raise ValueError(89 'Role ARN must follow either the name or full arn patterns',90 IAM_ROLE_NAME,91 IAM_ROLE_ARN92 )93 elif isinstance(role, (Parameter, Role)):94 role_arn = GetAtt(role, 'Arn')95 elif isinstance(role, (GetAtt, Sub, Ref, ImportValue)):96 role_arn = role97 else:98 raise TypeError('role expected to be of type', str, ImportValue, Role, Sub, GetAtt, Ref)99 return role_arn100def lambda_function(function):101 """102 Args:103 function: represents the function object, or a function104 Returns:105 untouched if one of the functions supported106 string of the full ARN if the function name is given107 full ARN if full ARN is given and match function ARN pattern108 """109 arn_pattern = re.compile(LAMBDA_ARN)110 name_pattern = re.compile(LAMBDA_NAME)111 if isinstance(function, str):112 if name_pattern.match(function):113 function_arn = Sub(f'arn:aws:lambda:${{AWS::Region}}:${{AWS::AccountId}}:function:{function}')114 elif function.startswith('arn:aws:lambda:') and arn_pattern.match(function):115 function_arn = function116 else:117 raise ValueError(118 'Function ARN must follow either the name or full arn patterns',119 LAMBDA_NAME,120 LAMBDA_ARN121 )122 elif isinstance(function, (Parameter, Function)):123 function_arn = GetAtt(function, 'Arn')124 elif isinstance(function, (ImportValue, GetAtt, Sub, Ref)):125 function_arn = function126 else:127 raise TypeError('Function expected to be of type', str, Role, Sub, GetAtt, Ref, ImportValue)128 return function_arn129def lambda_layer(layer):130 """131 Args:132 layer: represents the layer object, or a function133 Returns:134 untouched if one of the functions supported135 string of the full ARN if the layer name is given136 full ARN if full ARN is given and match Lambda layer ARN pattern137 """138 arn_pattern = re.compile(LAMBDA_LAYER_ARN)139 version_pattern = re.compile(LAMBDA_LAYER_VERSION)140 if isinstance(layer, (GetAtt, Ref, Sub, ImportValue)):141 return layer142 elif isinstance(layer, str):143 if arn_pattern.match(layer):144 return layer145 elif version_pattern.match(layer):146 return Sub(f'arn:aws:lambda:${{AWS::Region}}:${{AWS::AccountId}}:layer:{layer}')147 else:148 raise ValueError(149 "Layer ARN expected of format"150 f"{LAMBDA_LAYER_ARN} or {LAMBDA_LAYER_VERSION}"151 )152 else:153 raise ValueError(154 'Layer does not comply to any required patterns of Functions'155 )156def kms_key(key):157 """158 Args:159 key: represents the key object, or a function160 Returns:161 untouched if one of the functions supported162 string of the full ARN if the key name is given163 full ARN if full ARN is given and match KMS key ARN pattern164 """165 arn_pattern = re.compile(KMS_KEY_ARN)166 id_pattern = re.compile(KMS_KEY_ID)167 if isinstance(key, (Ref, Sub, ImportValue, GetAtt)):168 return key169 if isinstance(key, (Parameter, Key)):170 return GetAtt(key, 'Arn')171 if isinstance(key, str):172 if arn_pattern.match(key):173 return key174 if id_pattern.match(key):175 return Sub(f'arn:aws:kms:${{AWS::Region}}:${{AWS::AccountId}}:key/{key}')176 else:177 raise ValueError('Key does not match pattern', KMS_KEY_ARN, KMS_KEY_ID)178def kms_alias(alias):179 """180 Args:181 alias: represents the alias object, or a function182 Returns:183 untouched if one of the functions supported184 string of the full ARN if the alias name is given185 full ARN if full ARN is given and match KMS Key alias ARN pattern186 """187 arn_pattern = re.compile(KMS_ALIAS_ARN)188 alias_pattern = re.compile(KMS_ALIAS)189 if isinstance(alias, (Ref, Sub, ImportValue, GetAtt)):190 return alias191 if isinstance(alias, (Parameter, Alias)):192 return GetAtt(alias, 'Arn')193 if isinstance(alias, str):194 if arn_pattern.match(alias):195 return alias196 if alias_pattern.match(alias):197 return Sub(f'arn:aws:kms:${{AWS::Region}}:${{AWS::AccountId}}:{alias}')198 else:...

Full Screen

Full Screen

setup_lambda_scoring_layer.py

Source:setup_lambda_scoring_layer.py Github

copy

Full Screen

12__version__ = '1.0'34import pkgutil5import boto367from awsdest.utils.core_aws import *8from awsdest.utils.core_aws_lambda import *91011if __name__ == '__main__':1213 awsconfig, k8sconfig, siteconfig, lambdaconfig = init_config()14 #print("awsconfig", awsconfig)15 #print("k8sconfig", k8sconfig)16 #print("site-specific: ", siteconfig)17 print("lambda-specific", lambdaconfig)18 print("#################")1920 lambda_layer_arn, lambda_layer_version = create_lambda_layer(awsconfig, lambdaconfig)2122 print("Lambda Layer ARN: ", lambda_layer_arn)23 print("Lambda Layer Version: ", lambda_layer_version)24 print("Above values needs to go config.properties so they can be picked by setup_lambda_scoring")25 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful