public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appSettings.json", optional: false, reloadOnChange: true) .AddJsonFile("appSettings." + env.EnvironmentName + ".json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); HostingEnvironment = env; // Serilog Serilog.Debugging.SelfLog.Enable(x => Debug.WriteLine(x)); var awsOptions = Configuration.GetAWSOptions(); // AWS clients _s3Client = awsOptions.CreateServiceClient <IAmazonS3>(); _lambdaClient = awsOptions.CreateServiceClient <IAmazonLambda>(); _cloudWatchClient = awsOptions.CreateServiceClient <IAmazonCloudWatchLogs>(); _logger = new LoggerConfiguration() .ReadFrom.Configuration(Configuration) .WriteTo.AmazonCloudWatch(new CloudWatchSinkOptions { LogGroupName = "markdown-webapi", Period = TimeSpan.FromSeconds(5) }, _cloudWatchClient) .CreateLogger(); _logger.Information("Server started: HostingEnvironment: {@HostingEvironment}", env); _logger.Information("Server started: Environment: {@Environment}", Environment.GetEnvironmentVariables()); _logger.Information("Server started: Configuration: {@Configuration}", Configuration.AsEnumerable()); _settings = new WebApiSettings(Configuration); _fileUploadSettings = new FileUploadSettings(Configuration); }
protected async Task UpdateHandlerAsync(IAmazonLambda lambdaClient, string handler, Dictionary <string, string> environmentVariables = null) { var updateFunctionConfigurationRequest = new UpdateFunctionConfigurationRequest { FunctionName = FunctionName, Handler = handler, Environment = new Model.Environment { IsVariablesSet = true, Variables = environmentVariables ?? new Dictionary <string, string>() } }; await lambdaClient.UpdateFunctionConfigurationAsync(updateFunctionConfigurationRequest); // Wait for eventual consistency of function change. var getConfigurationRequest = new GetFunctionConfigurationRequest { FunctionName = FunctionName }; GetFunctionConfigurationResponse getConfigurationResponse = null; do { await Task.Delay(1000); getConfigurationResponse = await lambdaClient.GetFunctionConfigurationAsync(getConfigurationRequest); } while (getConfigurationResponse.State == State.Pending); await Task.Delay(1000); }
public async Task <Response?> Wait() { var props = Request.ResourceProperties; IAmazonCertificateManager acmClient = await acmFactory.Create(props.CreationRoleArn); IAmazonLambda lambdaClient = await lambdaFactory.Create(); var request = new DescribeCertificateRequest { CertificateArn = PhysicalResourceId }; var response = await acmClient.DescribeCertificateAsync(request); var status = response?.Certificate?.Status?.Value; switch (status) { case "PENDING_VALIDATION": Thread.Sleep(WaitInterval * 1000); var invokeResponse = await lambdaClient.InvokeAsync(new InvokeRequest { FunctionName = Context.FunctionName, Payload = JsonSerializer.Serialize(Request, SerializerOptions), InvocationType = InvocationType.Event, }); break; case "ISSUED": return(new Response()); default: throw new Exception($"Certificate could not be issued. (Got status: {status})"); } return(null); }
public static string GetFunctionArnIfExists(IAmazonLambda lambdaClient, string functionName) { AutoResetEvent ars = new AutoResetEvent(false); Exception responseException = new Exception(); string functionArn = null; lambdaClient.GetFunctionAsync(new GetFunctionRequest() { FunctionName = functionName }, (response) => { responseException = response.Exception; if (responseException == null) { functionArn = response.Response.Configuration.FunctionArn; } ars.Set(); }, new AsyncOptions { ExecuteCallbackOnMainThread = false }); ars.WaitOne(); if (responseException == null) { Assert.IsNotNull(functionArn); } else { Assert.IsInstanceOf(typeof(ResourceNotFoundException), responseException); } return(functionArn); }
public InputsController(LogContext logContext, AccountContext accountContext, IAmazonS3 S3Client, IAmazonKinesisFirehose FirehoseClient, IAmazonLambda LambdaClient) { _logContext = logContext; _accountContext = accountContext; _S3Client = S3Client; _FirehoseClient = FirehoseClient; _LambdaClient = LambdaClient; }
//--- Constructors --- public LambdaRobotsBotClient(string botId, string lambdaArn, TimeSpan requestTimeout, IAmazonLambda lambdaClient = null, ILambdaSharpLogger logger = null) { _botId = botId ?? throw new ArgumentNullException(nameof(botId)); _lambdaArn = lambdaArn ?? throw new ArgumentNullException(nameof(lambdaArn)); _requestTimeout = requestTimeout; _lambdaClient = lambdaClient ?? new AmazonLambdaClient(); _logger = logger; }
static Entrypoint() { snsClient = new AmazonSimpleNotificationServiceClient(); priceListClient = new PriceListClient(); s3Client = new AmazonS3Client(); lambdaClient = new AmazonLambdaClient(); snsTopic = System.Environment.GetEnvironmentVariable("SNS"); }
/// <summary> /// Calls the asynchronous ListFunctionsAsync method of the Lambda /// client to retrieve the list of functions in the AWS Region with /// which the Lambda client was initialized. /// </summary> /// <param name="client">The initialized Lambda client which will be /// used to retrieve the list of Lambda functions.</param> /// <returns>A list of Lambda functions configuration information.</returns> public static async Task <List <FunctionConfiguration> > ListFunctionsAsync(IAmazonLambda client) { // Get the list of functions. The response will have a property // called Functions, a list of information about the Lambda // functions defined on your account in the specified region. var response = await client.ListFunctionsAsync(); return(response.Functions); }
public LambdaSource(IAmazonLambda client) { if (client == null) { throw new ArgumentNullException(nameof(client)); } _client = client; }
/// <summary> /// Contstructor used mainly for unit testing so the /// Amazon SDK calls can be mocked /// </summary> /// <param name="sns"></param> /// <param name="s3"></param> /// <param name="lambda"></param> public Entrypoint( IAmazonSimpleNotificationService sns, IAmazonS3 s3, IAmazonLambda lambda ) { snsClient = sns; s3Client = s3; lambdaClient = lambda; }
protected async Task <InvokeResponse> InvokeFunctionAsync(IAmazonLambda lambdaClient, string payload) { var request = new InvokeRequest { FunctionName = FunctionName, Payload = payload, LogType = LogType.Tail }; return(await lambdaClient.InvokeAsync(request)); }
private async Task InvokeLoggerTestController(IAmazonLambda lambdaClient) { var payload = File.ReadAllText("get-loggertest-request.json"); var response = await InvokeFunctionAsync(lambdaClient, payload); Assert.Equal(200, response.StatusCode); var apiGatewayResponse = System.Text.Json.JsonSerializer.Deserialize <APIGatewayHttpApiV2ProxyResponse>(response.Payload); Assert.Contains("90000", apiGatewayResponse.Body); }
private void InitHooks() { _lambda = _lambda ?? new AmazonLambdaClient(); _codeDeploy = _codeDeploy ?? new AmazonCodeDeployClient(); _httpClient = _httpClient ?? new HttpClient(new AwsSignedHttpMessageHandler { InnerHandler = new HttpClientHandler() }) { BaseAddress = Env.Get("ApiBaseUrl").ToUri() }; }
//--- Methods --- public override async Task InitializeAsync(LambdaConfig config) { _table = new DynamoTable( config.ReadDynamoDBTableName("GameTable"), new AmazonDynamoDBClient() ); _gameStateMachine = config.ReadText("GameLoopStateMachine"); _stepFunctionsClient = new AmazonStepFunctionsClient(); _lambdaClient = new AmazonLambdaClient(); _gameTurnFunctionArn = config.ReadText("GameTurnFunction"); }
public static void Main(string[] args) { CognitoAWSCredentials credentials = new CognitoAWSCredentials( "us-east-1:xx0000x0-0x00-0000-0000-0x0000000xxx", // Identity pool ID RegionEndpoint.USEast1 // Region ); lambdaClient = new AmazonLambdaClient(credentials, RegionEndpoint.USEast1); RunMainLoop(); }
private async Task InvokeSuccessToWeatherForecastController(IAmazonLambda lambdaClient) { var payload = File.ReadAllText("get-weatherforecast-request.json"); var response = await InvokeFunctionAsync(lambdaClient, payload); Assert.Equal(200, response.StatusCode); var apiGatewayResponse = System.Text.Json.JsonSerializer.Deserialize <APIGatewayHttpApiV2ProxyResponse>(response.Payload); Assert.Equal("application/json; charset=utf-8", apiGatewayResponse.Headers["Content-Type"]); Assert.Contains("temperatureC", apiGatewayResponse.Body); }
protected async Task <bool> PrepareTestResources(IAmazonS3 s3Client, IAmazonLambda lambdaClient, AmazonIdentityManagementServiceClient iamClient) { var roleAlreadyExisted = await ValidateAndSetIamRoleArn(iamClient); var testBucketName = TestBucketRoot + Guid.NewGuid().ToString(); await CreateBucketWithDeploymentZipAsync(s3Client, testBucketName); await CreateFunctionAsync(lambdaClient, testBucketName); return(roleAlreadyExisted); }
public void Deconstruct( out IAmazonRDS rdsClient, out IAmazonLambda lambdaClient, out IAmazonEventBridge eventsClient, out WaitForDatabaseAvailabilityHandler handler ) { rdsClient = RdsClient; lambdaClient = LambdaClient; eventsClient = EventBridgeClient; handler = StartDatabaseHandler; }
public RouterFunction(RouterConfig config) { AWSSDKHandler.RegisterXRayForAllServices(); _lambda = new AmazonLambdaClient(); _sqs = new AmazonSQSClient(); foreach (var entry in config.Routes) { _router.Register(entry.ServiceKey, entry.Route, entry); } }
/// <summary> /// Serializes a payloadObject object and invokes it against the supplied function name. /// </summary> /// <typeparam name="T">The payloadObject object </typeparam> /// <param name="client"></param> /// <param name="functionName"></param> /// <param name="payloadObject"></param> /// <returns></returns> public static Task <InvokeResponse> InvokeRequestAsync <T>(this IAmazonLambda client, string functionName, T payloadObject) { var payload = JsonSerializer.Serialize(payloadObject); var invokeRequest = new InvokeRequest { FunctionName = functionName, InvocationType = InvocationType.RequestResponse, Payload = payload }; return(client.InvokeAsync(invokeRequest)); }
static async Task <(string functionArn, string functionName, Func <Task> functionCleanup)> CreateFunction( string roleArn, IAmazonLambda lambda, LambdaCreationOptions options) { var functionName = $"Beetles_{options.Name}_{options.Timestamp}"; var retryWait = Stopwatch.StartNew(); CreateFunctionResponse function = null; do { try { function = await lambda.CreateFunctionAsync(new CreateFunctionRequest { Code = new FunctionCode { ZipFile = new MemoryStream(File.ReadAllBytes(options.PackagePath)) }, Description = $"Beetles Load Test {options.Name}", FunctionName = functionName, Handler = options.LambdaHandlerName, MemorySize = options.MemorySize, Publish = true, Role = roleArn, Runtime = "dotnetcore2.1", Timeout = 120, Environment = new Environment { Variables = { [Constants.ConfigurationTypeNameKey] = options.SettingsTypeName } }, VpcConfig = new VpcConfig() }, options.CancellationToken); // await lambda.PutFunctionConcurrencyAsync(new PutFunctionConcurrencyRequest() // { // FunctionName = functionName, // ReservedConcurrentExecutions = options.ProvisionedConcurrency // }, options.CancellationToken); } catch (InvalidParameterValueException) when(retryWait.Elapsed < TimeSpan.FromMinutes(1)) { } } while (function == null); var functionArn = function.FunctionArn; return(functionArn, functionName, async() => await lambda.DeleteFunctionAsync(functionName)); }
public static async Task WaitTillFunctionAvailableAsync(IToolLogger logger, IAmazonLambda lambdaClient, string functionName) { const int POLL_INTERVAL = 3000; const int MAX_TIMEOUT_MINUTES = 20; try { var request = new GetFunctionConfigurationRequest { FunctionName = functionName }; GetFunctionConfigurationResponse response = null; bool logInitialMessage = false; var timeout = DateTime.UtcNow.AddMinutes(MAX_TIMEOUT_MINUTES); var startTime = DateTime.UtcNow; do { response = await lambdaClient.GetFunctionConfigurationAsync(request); if (response.LastUpdateStatus != LastUpdateStatus.InProgress && response.State != State.Pending) { if (response.LastUpdateStatus == LastUpdateStatus.Failed) { // Not throwing exception because it is possible the calling code could be fixing the failed state. logger.WriteLine($"Warning: function {functionName} is currently in failed state: {response.LastUpdateStatusReason}"); } return; } if (!logInitialMessage) { logger.WriteLine($"An update is currently in progress for Lambda function {functionName}. Waiting till update completes."); logInitialMessage = true; } else { var ts = DateTime.UtcNow - startTime; logger.WriteLine($"... Waiting ({ts.TotalSeconds.ToString("N2")} seconds)"); } await Task.Delay(POLL_INTERVAL); } while (DateTime.UtcNow < timeout); } catch (Exception e) { throw new LambdaToolsException($"Error waiting for Lambda function to be in available status: {e.Message}", LambdaToolsException.LambdaErrorCode.LambdaWaitTillFunctionAvailable); } throw new LambdaToolsException($"Timeout waiting for function {functionName} to become available", LambdaToolsException.LambdaErrorCode.LambdaWaitTillFunctionAvailable); }
//--- Methods --- public override async Task InitializeAsync(LambdaConfig config) { // initialize Lambda function _lambdaClient = new AmazonLambdaClient(); _amaClient = new AmazonApiGatewayManagementApiClient(new AmazonApiGatewayManagementApiConfig { ServiceURL = config.ReadText("Module::WebSocket::Url") }); _table = new DynamoTable( config.ReadDynamoDBTableName("GameTable"), new AmazonDynamoDBClient() ); _gameApiUrl = config.ReadText("RestApiUrl"); }
protected async Task CreateFunctionAsync(IAmazonLambda lambdaClient, string bucketName) { await DeleteFunctionIfExistsAsync(lambdaClient); var createRequest = new CreateFunctionRequest { FunctionName = FunctionName, Code = new FunctionCode { S3Bucket = bucketName, S3Key = DeploymentZipKey }, Handler = "PingAsync", MemorySize = 512, Timeout = 30, Runtime = Runtime.ProvidedAl2, Role = ExecutionRoleArn }; var startTime = DateTime.Now; var created = false; while (DateTime.Now < startTime.AddSeconds(30)) { try { await lambdaClient.CreateFunctionAsync(createRequest); created = true; break; } catch (InvalidParameterValueException ipve) { // Wait for the role to be fully propagated through AWS if (ipve.Message == "The role defined for the function cannot be assumed by Lambda.") { await Task.Delay(2000); } else { throw; } } } await Task.Delay(5000); if (!created) { throw new Exception($"Timed out trying to create Lambda function {FunctionName}"); } }
public WaitForDatabaseAvailabilityHandler( IAmazonRDS rdsClient, IAmazonLambda lambdaClient, IAmazonEventBridge eventsClient, ILogger <WaitForDatabaseAvailabilityHandler> logger, IOptions <LambdaConfiguration> configuration ) { this.rdsClient = rdsClient; this.lambdaClient = lambdaClient; this.eventsClient = eventsClient; this.logger = logger; this.configuration = configuration.Value; }
private async Task KeepAlive(int concurrency, string invocationType) { _lambda = _lambda ?? new AmazonLambdaClient(); var tasks = new List <Task <InvokeResponse> >(); LambdaLogger.Log("Concurrency " + concurrency); for (var i = 0; i < concurrency; i++) { LambdaLogger.Log("Invoke " + i); tasks.Add(_lambda.InvokeAsync(CreateInvokeRequest(invocationType))); } await Task.WhenAll(tasks); }
public LambdaSender(string routerQueueName, string routerFunctionName) { _routerFunctionName = routerFunctionName; _lambda = new AmazonLambdaClient(); _sqs = new AmazonSQSClient(); _routerQueueUrl = new Lazy <Task <string> >(() => Task.Run(async() => { var response = await _sqs.GetQueueUrlAsync(routerQueueName); return(response.QueueUrl); })); }
public void Deconstruct( out IAmazonRDS rdsClient, out IAmazonSimpleNotificationService snsClient, out IAmazonEventBridge eventsClient, out IAmazonSQS sqsClient, out IAmazonLambda lambdaClient, out StartDatabaseHandler handler ) { rdsClient = RdsClient; snsClient = SnsClient; eventsClient = EventBridgeClient; sqsClient = SqsClient; lambdaClient = LambdaClient; handler = StartDatabaseHandler; }
protected async Task DeleteFunctionIfExistsAsync(IAmazonLambda lambdaClient) { var request = new DeleteFunctionRequest { FunctionName = FunctionName }; try { var response = await lambdaClient.DeleteFunctionAsync(request); } catch (ResourceNotFoundException) { // no problem } }
protected void Page_Load(object sender, EventArgs e) { // Get the number the SMS came from, string fromNumber = Request["From"]; // Create the client to be used to invoke the lambda function. // This will use the default credentials from the profile store. var config = new AmazonLambdaConfig { RegionEndpoint = RegionEndpoint.EUWest1 }; IAmazonLambda client = AWSClientFactory.CreateAmazonLambdaClient(config); // Arguments for the lambda function - the number the SMS came from. string arguments = @"{""to"": """ + fromNumber + @"""}"; // Invoke the lambda function which will send a reply to the SMS message. client.InvokeAsync("SimpleSMS", arguments); }
public static string GetFunctionArnIfExists(IAmazonLambda lambdaClient, string functionName) { AutoResetEvent ars = new AutoResetEvent(false); Exception responseException = new Exception(); string functionArn = null; lambdaClient.GetFunctionAsync(new GetFunctionRequest() { FunctionName = functionName }, (response) => { responseException = response.Exception; if (responseException == null) { functionArn = response.Response.Configuration.FunctionArn; } ars.Set(); }, new AsyncOptions { ExecuteCallbackOnMainThread = false }); ars.WaitOne(); if (responseException == null) { Assert.IsNotNull(functionArn); } else { Assert.IsInstanceOf(typeof(ResourceNotFoundException), responseException); } return functionArn; }
public static void ClassInitialize(TestContext testContext) { lambdaClient = new AmazonLambdaClient(); iamClient = new AmazonIdentityManagementServiceClient(); }